Under construction

Still being built. Things may change

Stamps

Interview

Time limit1sMemory limit1024 MB

Summary
Given stamp values and a limit on how many stamps fit on an envelope, find the longest run of consecutive postage values that can be made.
Level

Medium4 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

You are given a set of stamp values (for example {1, 3, 5}) and the maximum number of stamps that can be placed on an envelope (for example, five stamps). Your goal is to find the largest contiguous range of postage that can be made by combining the stamps.

For the set {1, 3, 5} with at most five stamps, the values can be made as follows:

  • 1: 1
  • 2: 1+1
  • 3: 3
  • 4: 1+3
  • 5: 5
  • 6: 1+5
  • 7: 5+1+1
  • 8: 5+3
  • 9: 5+3+1
  • 10: 5+5
  • 11: 5+5+1
  • 12: 5+5+1+1
  • 13: 5+5+3
  • 14: 5+5+3+1
  • 15: 5+5+5
  • 16: 5+5+3+3
  • 17: 5+5+5+1+1
  • 18: 5+5+5+3
  • 19: 5+5+5+3+1
  • 20: 5+5+5+5
  • 21: 5+5+5+3+3
  • 22: ????

No combination of at most five of these stamps makes 22. So the values 1 through 21 can be made, a total of 21 contiguous values. The largest contiguous range does not have to start at 1.

Input

The first line contains two space-separated integers S (1≤S≤201 \le S \le 20), the number of stamp types, and E (1≤E≤101 \le E \le 10), the maximum number of stamps that can fit on an envelope.

The second line contains S different space-separated positive integers, the stamp values (1≤stampval≤10001 \le \text{stampval} \le 1000).

Output

Print the maximum number of contiguous values that can be made by combining the stamps.

Examples1

  1. Example 1

    Input
    3 5
    1 3 5
    
    Expected output
    21