#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 505, MAX = 30;
int n, a[N], dp[N][MAX];
bitset <MAX> diff[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
dp[0][a[0]] = a[0];
diff[0][a[0]] = diff[0][0] = 1;
// cout << diff[0][1] << "\n";
// cout << 0 << " : " << diff[0] << "\n";
for (int i = 1; i < n; i++){
vector <int> buff;
for (int j = 0; j < MAX; j++)
if (diff[i - 1][j])
buff.push_back(j);
// for (auto u: buff) cout << u << " "; cout << "\n";
for (auto u: buff){
dp[i][u] = dp[i - 1][u];
if (u + a[i] < MAX)
dp[i][u + a[i]] = max(dp[i][u + a[i]], dp[i - 1][u] + a[i]);
if (u - a[i] >= 0)
dp[i][u - a[i]] = max(dp[i][u - a[i]], dp[i - 1][u] + a[i]);
else
dp[i][a[i] - u] = max(dp[i][a[i] - u], dp[i - 1][u] + a[i]);
}
diff[i] = (diff[i - 1]<<a[i]) | (diff[i - 1]>>a[i]);
for (int j = 0; j <= a[i]; j++) if (diff[i - 1][j]) diff[i][a[i] - j] = 1;
diff[i][a[i]] = 1;
}
int nsum = 0;
for (int i = n - 1; i >= 0; i--){
// cout << i << " : " << diff[i][0] << "\n";
if (dp[i][0]){
cout << dp[i][0] / 2 + sum - dp[i][0];
return 0;
}
// nsum += a[i];
}
}
Compilation message
kas.cpp: In function 'int main()':
kas.cpp:49:9: warning: unused variable 'nsum' [-Wunused-variable]
int nsum = 0;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |