题目描述

6 = 3 + 3
7 = 3 + 3 + 1
8 = 3 + 3 + 1 + 1
9 = 3 + 3 + 3
10 = 3 + 3 + 3 + 1
11 = 3 + 3 + 3 + 1 + 1
12 = 3 + 3 + 3 + 3
13 = 3 + 3 + 3 + 3 + 1


输入输出样例

5 2
1 3

13

说明/提示

USACO Training Section 3.1

题解

 1 #include <iostream>
2 #include <stdio.h>
3 #include <math.h>
4 #include <algorithm>
5 #include <string.h>
6
7 using namespace std;
8
9 const int    MAXN = 2000005;
10 int        a, n, k, f[MAXN];
11
12 int main()
13 {
14     cin >> k >> n;
15     for ( int i = 0; i < MAXN; i++ )
16     {
17         f[i] = 1111111;
18     }
19     f[0] = 0;
20     for ( int i = 1; i <= n; i++ )
21     {
22         cin >> a;
23         for ( int j = a; j <= MAXN; j++ )
24         {
25             if ( f[j - a] + 1 <= k ) /* 用的邮票数目在范围内 */
26             {
27                 f[j] = min( f[j], f[j - a] + 1 );
28             }
29         }
30     }
31     for ( int i = 1; i < MAXN; i++ )
32     {
33         if ( f[i] == 1111111 )
34         {
35             cout << i - 1 << endl;
36             break;
37         }
38     }
39     return(0);
40 }

posted on 2019-08-31 20:59  zealsoft  阅读(52)  评论(0编辑  收藏