#arc150f. [arc150_f]Constant Sum Subsequence
[arc150_f]Constant Sum Subsequence
Problem Statement
We have a sequence of positive integers of length , , and a positive integer . For this sequence of positive integers, holds for positive integers , and only are given as the input.
Find the minimum integer such that every sequence of positive integers totaling is a (not necessarily contiguous) subsequence of the sequence of positive integers.
It can be shown that at least one such exists under the Constraints of this problem.
Constraints
- For every positive integer , contains at least one occurrence of .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
6 4
1 1 2 1 4 3
Sample Output 1
9
There are eight sequences to consider: $B=(1,\\ 1,\\ 1,\\ 1),\\ (1,\\ 1,\\ 2),\\ (1,\\ 2,\\ 1),\\ (1,\\ 3),\\ (2,\\ 1,\\ 1),\\ (2,\\ 2),\\ (3,\\ 1),\\ (4)$.
For , for instance, is not a subsequence of $(A_1,A_2,\\ \\dots,\\ A_8)=(1,\\ 1,\\ 2,\\ 1,\\ 4,\\ 3,\\ 1,\\ 1)$.
For , on the other hand, every is a subsequence of $(A_1,A_2,\\ \\dots,\\ A_9)=(1,\\ 1,\\ 2,\\ 1,\\ 4,\\ 3,\\ 1,\\ 1,\\ 2)$.
Sample Input 2
14 5
1 1 1 2 3 1 2 4 5 1 1 2 3 1
Sample Output 2
11
Sample Input 3
19 10
1 6 2 7 4 8 5 9 1 10 4 1 3 1 3 2 2 2 1
Sample Output 3
39