标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14476 Accepted Submission(s):
5032
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[1010],p[1010]; int n,m; int main() { while(scanf("%d",&n)!=EOF&&n) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { scanf("%d",&p[i]); } sort(p,p+n); scanf("%d",&m); if(m<5) { printf("%d\n",m); continue; } m-=5; for(int i=0;i<n-1;i++) { for(int j=m;j>=p[i];j--) { dp[j]=max(dp[j],dp[j-p[i]]+p[i]); } } printf("%d\n",m+5-dp[m]-p[n-1]); } return 0; }
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,m; int dp[1010]; int p[1010]; void solve() { if(m<5) printf("%d\n",m); else { m-=5; for(int i=0;i<n-1;i++) { for(int j=m;j>=p[i];j--) { dp[j]=max(dp[j],dp[j-p[i]]+p[i]); } } printf("%d\n",m+5-p[n-1]-dp[m]); } } int main() { while(scanf("%d",&n)!=EOF&&n) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { scanf("%d",&p[i]); } sort(p,p+n); scanf("%d",&m); solve(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/qianyanwanyu--/p/4437125.html