#P2725. [USACO3.1] 邮票 Stamps

    ID: 1733 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>模拟动态规划,dp搜索贪心USACO背包

[USACO3.1] 邮票 Stamps

Description

Given a set of nn stamp denominations and an upper bound kk — meaning you may affix at most kk stamps to an envelope — find the largest positive integer mm such that every value from 11 to mm can be represented using no more than kk stamps.

Input Format

The first line contains two integers, the stamp limit kk and the number of denominations nn.

Starting from the second line, except for the last line, each line contains 1515 integers aia_i. The last line contains at most 1515 integers. In total there are nn integers; the ii-th integer is the value aia_i of the ii-th type of stamp.

Output Format

Output one line with a single integer mm. If mm does not exist, output 00.

5 2
1 3
13

Hint

Sample Input/Output 1 Explanation

There are denominations 11 and 33; you may use at most 55 stamps. It is easy to form values 11 through 55 (just use the 11-value stamps). The following values are also easy:

  • 6=3+36 = 3 + 3.
  • 7=3+3+17 = 3 + 3 + 1.
  • 8=3+3+1+18 = 3 + 3 + 1 + 1.
  • 9=3+3+39 = 3 + 3 + 3.
  • 10=3+3+3+110 = 3 + 3 + 3 + 1.
  • 11=3+3+3+1+111 = 3 + 3 + 3 + 1 + 1.
  • 12=3+3+3+312 = 3 + 3 + 3 + 3.
  • 13=3+3+3+3+113 = 3 + 3 + 3 + 3 + 1.

However, using 55 stamps of values 11 or 33, it is impossible to form 1414. Therefore, the answer is 1313.

Constraints

  • For 100%100\% of the testdata, 1k2001 \leq k \leq 200, 1n501 \leq n \leq 50, 1ai1041 \leq a_i \leq 10^4.

Note

  • The statement is translated from NOCOW.

Translated by ChatGPT 5