Fewest exercises to get tired
You start with E energy. Exercise i uses A[i] energy, and each exercise can be done at most twice. You are tired when your energy reaches 0 or less. Find the fewest exercises needed to get tired, or -1 if doing every exercise twice is not enough.
Reported in: Infosys (SP / DSE · earlier papers) (memory-based)
Input
First line: E. Second line: N. Third line: N values.
Output
The fewest exercises, or -1.
Constraints
1 ≤ N ≤ 10^5; 1 ≤ E, A[i] ≤ 10^9.
Example 1
Input:
6
2
1 2
Output:
4Example 2
Input:
10
3
1 5 2
Output:
2Example 3
Input:
100
2
3 4
Output:
-1Hint: Do the most draining exercise first (twice), then the next one, and so on, until the total reaches E.