Source Code
#include <iostream>
#include <stack>
using namespace std;

int main() {
	int n, strength;
	int initial[100000];
	int heights[100000];
	
	cin >> n >> strength;
	for (int i=0; i<n; i++) {
		cin >> initial[i];
		heights[i] = initial[i];
	}
	int counter = 0;
	int pos = 0;
	stack<int> temp;
	while (true) {
		if (heights[pos] > strength)
			break;
			
		strength -= heights[pos];
		counter++;
		heights[pos] += initial[pos];
		temp.push(heights[pos]);
		
		pos++;
		
		if (pos == n) {
			while (!temp.empty()) {
				if (temp.top() <= strength) {
					pos--;
					strength -= heights[pos];
					counter++;
					heights[pos] += initial[pos];
					temp.pop();
				}
				else {
					break;
				}
			}
		}
	}
	
	cout << counter;
}
Copy
Saqqa 26/40 Dana
GNU G++17
2049 ms
1.9 MB
Time Limit Exceeded