Bridge Transport
Problem Description
A train has a total of N carriages, numbered from 1 to N from front to back.
Each carriage is 10 meters long, but the weight of each carriage may vary.
Now, the train needs to cross a bridge that is 40 meters long.
The maximum load capacity of the bridge is W. If the total weight of the carriages on the bridge exceeds this value, the bridge will collapse.
To ensure the train can safely cross the bridge, it may be necessary to remove some carriages from the rear, retaining only the front carriages.
Please calculate the maximum number of carriages that can be retained.
Input Format
- The first line contains the integer W
- The second line contains the integer N
- The next N lines, where the i-th line contains an integer wi, represent the weight of the i-th carriage
Output Format
An integer, representing the maximum number of carriages that can be retained.
Constraints
- 1 ≤ W ≤ 105
- 1 ≤ N ≤ 105
- 1 ≤ wi ≤ 105
Examples
Sample Input 1
100
6
50
30
10
10
40
50
Sample Output 1
5
Explanation: The maximum load capacity of the bridge is 100.
The total weight of carriages 1-4 is 50 + 30 + 10 + 10 = 100, which does not exceed the maximum load.
The total weight of carriages 2-5 is 30 + 10 + 10 + 40 = 90, which does not exceed the maximum load.
The total weight of carriages 3-6 is 10 + 10 + 40 + 50 = 110, which exceeds the maximum load.
Therefore, by removing the 6th carriage and retaining the first 5 carriages, the train can safely cross the bridge.
Sample Input 2
100
3
150
1
1
Sample Output 2
0
Explanation: The maximum load capacity of the bridge is 100. However, the weight of the first carriage, 150, already exceeds this maximum load. Therefore, no carriages can be retained.
Comments