标签:
5
1 3 4 5 2
7
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; int n,a[105],f[105][2005]; int main(){ cin>>n; for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 0;i <= n;i++){ for(int j = 0;j <= 2000;j++){ f[i][j] = -1; } } f[0][0] = 0; for(int i = 1;i <= n;i++){ for(int j = 0;j <= 2000;j++){ f[i][j] = max(f[i][j],f[i-1][j]); if(f[i-1][j] != -1){ f[i][j+a[i]] = max(f[i][j+a[i]],f[i-1][j]); if(a[i] <= j) f[i][j-a[i]] = max(f[i][j-a[i]],f[i-1][j] + a[i]); else f[i][a[i]-j] = max(f[i][a[i]-j],f[i-1][j] + j); } } } if(f[n][0]) cout<<f[n][0]; else cout<<"Impossible"; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5791420.html