Stamps
InterviewTime limit1sMemory limit1024 MB
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 (), the number of stamp types, and E (), the maximum number of stamps that can fit on an envelope.
The second line contains S different space-separated positive integers, the stamp values ().
Output
Print the maximum number of contiguous values that can be made by combining the stamps.