https://vjudge.net/contest/150953#problem/B
转化一下就行了,<5时要特判
#include<map> #include<set> #include<list> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; const int maxn=600,N=1005; int main() { int t,n,W,v[N],dp[N]; while(cin>>n,n){ memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++)cin>>v[i]; cin>>W; sort(v,v+n); if(W<5) { cout<<W<<endl; continue; } for(int i=0;i<n-1;i++) for(int j=W-5;j>=v[i];j--) dp[j]=max(dp[j],dp[j-v[i]]+v[i]); cout<<W-dp[W-5]-v[n-1]<<endl; } return 0; }