#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn=1e5+10;
const int mod=1e9+7;
int n;
ll a[503];
ll dp[maxn], sum=0; ///dp[i] is the max sum of the subsequences that when split optimally, the 2 parts differ by i
ll curr[maxn];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for(int i=0; i<=sum; i++){
dp[i]=-INT_MAX;
}
dp[0]=0;
cin>>n;
for(int i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
for(int j=0; j<=sum; j++){
curr[j]=dp[j];
}
for(int j=a[i]; j<=sum; j++){
if(dp[j-a[i]]==-INT_MAX) continue;
curr[j]=max(curr[j], dp[j-a[i]]+a[i]);
}
for(int j=0; j<=sum-a[i]; j++){
if(dp[j+a[i]]==-INT_MAX) continue;
curr[j]=max(curr[j], dp[j+a[i]]);
}
for(int j=1; j<a[i]; j++){
if(dp[j]==-INT_MAX) continue;
curr[a[i]-j]=max(curr[a[i]-j], dp[j]-j+a[i]);
}
for(int j=0; j<=sum; j++){
dp[j]=max(curr[j], dp[j]);
}
}
cout<<sum-dp[0]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1876 KB |
Output is correct |
2 |
Correct |
11 ms |
1924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
1876 KB |
Output is correct |
2 |
Correct |
15 ms |
1768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1868 KB |
Output is correct |
2 |
Incorrect |
74 ms |
1768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |