Skip to content

Commit 75b5f77

Browse files
authored
Merge pull request #1280 from AlgorithmWithGod/khj20006
[20251031] BOJ / P5 / 이혼 / 권혁준
2 parents e0e54cf + e90f06c commit 75b5f77

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N, arr[24]{}, ans = 0;
6+
map<int, int> le;
7+
8+
void init() {
9+
for(int i=0;i<N;i++) cin>>arr[i];
10+
ans = 0;
11+
le = map<int, int>();
12+
}
13+
14+
void fillLeft(int pos, int tar, int cur, int sum) {
15+
le[cur] = max(le[cur], sum);
16+
if(pos == tar) return;
17+
fillLeft(pos+1, tar, cur, sum);
18+
sum += arr[pos];
19+
fillLeft(pos+1, tar, cur+arr[pos], sum);
20+
fillLeft(pos+1, tar, cur-arr[pos], sum);
21+
}
22+
23+
void checkRight(int pos, int tar, int cur, int sum) {
24+
if(le.find(cur) != le.end()) ans = max(ans, le[cur] + sum);
25+
if(pos == tar) return;
26+
checkRight(pos+1, tar, cur, sum);
27+
sum += arr[pos];
28+
checkRight(pos+1, tar, cur+arr[pos], sum);
29+
checkRight(pos+1, tar, cur-arr[pos], sum);
30+
}
31+
32+
int solve() {
33+
fillLeft(0,N>>1,0,0);
34+
checkRight(N>>1,N,0,0);
35+
int sum = 0;
36+
for(int i=0;i<N;i++) sum += arr[i];
37+
return sum - ans;
38+
}
39+
40+
int main() {
41+
cin.tie(0)->sync_with_stdio(0);
42+
43+
for(cin>>N;N;cin>>N) {
44+
init();
45+
cout<<solve()<<'\n';
46+
}
47+
48+
}
49+
```

0 commit comments

Comments
 (0)