Description
Input
Output
Sample Input
1 50 5 10 1 2 3 2 1 1 2 3 2 1 50 0
Sample Output
-45 32
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,m,a[100217]; int dp[1005][1005]; int main() { while(scanf("%d",&n),n!=0) { memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); a[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); int maxx=a[n]; scanf("%d",&m); if(m<5) { printf("%d\n",m); continue; } m=m-5; for(int i=1;i<n;i++) { for(int j = m;j >= 0; j--) { dp[i][j]=dp[i-1][j]; if(j>=a[i]) dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i]]+a[i]); } } printf("%d\n",m+5-dp[n-1][m]-maxx); } return 0; } /* 2 145 200 150 3 144 200 900 150 */