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;
	while (true) {
		if (pos < 0)
			pos = 0;
			
		if (heights[pos] > strength)
			break;
			
		strength -= heights[pos];
		counter++;
		heights[pos] += initial[pos];
		
		pos++;
		
		if (pos == n) {
			pos--;
			while (pos >= 0) {
				if (heights[pos] > strength)
					break;
			
				strength -= heights[pos];
				counter++;
				heights[pos] += initial[pos];
				pos--;
			}
		}
	}
	
	cout << counter;
}
Copy
Saqqa 26/40 Dana
GNU G++17
2048 ms
1.2 MB
Time Limit Exceeded