[Usaco 3.1.6] stamps [same as above: full backpack] csust 1084

Source: Internet
Author: User

1084: [usaco 3.1.6] stamps

Time Limit: 1 sec memory limit: 64 MB
Submit: 122 solved: 33 submitstatusweb Board

Description

It is known that a set of face values (for example, {1 point, 3 points}) of N stamps and an upper limit k indicate that K stamps can be attached to an envelope. Calculate the maximum continuous postage from 1 to M. For example, suppose there are stamps of one or three points; you can paste up to five stamps. It is easy to post a postage of 1 to 5 points (just paste it with a one-point stamp), and the next postage is not hard:

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.

However, using five stamps with one or three points is impossible to post a postage of 14 points. Therefore, for the set of the two stamps and the maximum K = 5, the answer is M = 13.

Input

Row 1st: two integers, K and N. K (1 <= k <= 200) is the total number of available stamps. N (1 <= n <= 50) is the number of stamps. Row 2nd .. end of the file: N integers, 15 in each line, list the face values of all N stamps, with a face value of no more than 10000.

Output

Row 1st: an integer that indicates the number of postage stamps attached to no more than K stamps in a continuous available set starting from 1.

Sample Input

5 21 3

Sample output

13

Hint

Source

Usaco train

Algorithm: DP full backpack (each stamp can be selected countless times)


Ideas: Calculates the minimum number of stamps required for a combination of stamps (expressed by DP [I] as the minimum number of stamps required for combination into a face value I)

Traverse DP [I] from small to large in turn, once more than K is encountered, exit, and finally output the I-1.

For the two-dimensional DP [I] [J] (the first basic stamp face value is used as the minimum number of stamps required for J)

The previous questions have been analyzed clearly and I will not go into details here.


State transition equation:

DP [J] = min (DP [J], DP [J-A] + 1 );

Minimum number of stamps whose face value is I = Minimum (if the current stamp is not selected, select the current stamp)


Pseudocode:

I: 0... n-1

J: A [I]... Max

DP [J] = min (DP [J], DP [J-A [I] + 1 );


1084 Accepted 8728 211 C ++/edit 706 B 19:40:54

# Include <stdio. h ># include <algorithm> using namespace STD; const int maxn = 200*10000 + 10; int DP [maxn]; int main () {int K, N; while (scanf ("% d", & K, & N )! = EOF) {for (INT I = 0; I <maxn; I ++) // each initialization fails to meet DP [I] = k + 1; DP [0] = 0; int A; For (INT I = 0; I <n; I ++) {scanf ("% d", & ); for (Int J = A; j <maxn; j ++) // traverse to the possible maximum nominal value DP [J] = min (DP [J], DP [J-A] + 1) ;}int I = 1; for (I = 1; I <maxn; I ++) {If (DP [I]> K) break; // once not met, it jumps out} printf ("% d \ n", I-1); // maximum fulfillment} return 0 ;}

Same

1084 Accepted 8728 240 C ++/edit 913 B 19:48:04
# Include <stdio. h ># include <algorithm> using namespace STD; const int maxn = 200*10000 + 10; int A [55]; int DP [maxn]; int main () {int K, N; while (scanf ("% d", & K, & N )! = EOF) {int max = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]); max = max (max, a [I]);} DP [0] = 0; int maxans = K * max; // maximum possible value for (INT I = 1; I <= maxans; I ++) DP [I] = k + 1; // each initialization fails to meet the for (INT I = 0; I <n; I ++) // complete backpack: Find the minimum number of stamps required to generate J {for (Int J = A [I]; j <= maxans; j ++) {DP [J] = min (DP [J], DP [J-A [I] + 1); // do not select the I stamp, comparison with the I stamp selected} int I = 0; for (I = 1; I <= maxans; I ++) {If (DP [I]> K) break; // from small to large find once the required minimum number of votes greater than K immediately quit} printf ("% d \ n", I-1);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.