# 饭卡

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49991    Accepted Submission(s): 16936

Problem Description

Input

n=0表示数据结束。

Output

Sample Input
1 50 5 10 1 2 3 2 1 1 2 3 2 1 50 0

Sample Output
-45 32

AC代码如下：
``` 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 using namespace std;
5
6 int main()
7 {
8     int n,a[1100],num;
9     int dp[5000];
10     while(scanf("%d",&n)!=EOF&&n!=0)
11     {
12         for(int i=1;i<=n;i++)
13         {
14             scanf("%d",&a[i]);
15         }
16         sort(a+1,a+1+n);
17         scanf("%d",&num);
18         if(num<5){
19             printf("%d\n",num);
20             continue;
21         }
22         num-=5;
23         memset(dp,0,sizeof(dp));
24         for(int i=1;i<n;i++)
25         {
26             for(int j=num;j>=a[i];j--)
27             {
28                 dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
29             }
30         }
31         printf("%d\n",num+5-a[n]-dp[num]);
32     }
33     return 0;
34 }```
View Code