Source Code
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

long long a[100002];
long long p[100002];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 
    cout.tie(NULL);
	long long n, m;
	cin >> n >> m;
	long long k = m;
	for (int i = 0;i < n;i++) {
		cin >> a[i];
		p[i + 1] = p[i] + a[i];
	}
	long long t = 1;
	while ((p[n] * t) <= k) {
		k -= (p[n] * t);
		t++;
	}
	t--;
	m = k;
	long long ans = n * t;
	t++;
	if (t % 2) {
		for (int i = 0;i < n;i++) {
			if ((a[i] * (t)) <= m) {
				ans++;
				m -= (a[i] * (t));
			}
			else {
				break;
			}
		}
	}
	else {
		for (int i = n - 1;i > 0;i--) {
			if ((a[i] * (t)) <= m) {
				ans++;
				m -= (a[i] * (t));
			}
			else {
				break;
			}
		}
	}
	cout << ans;
}
Copy
Saqqa 26/40 lafi-Odeh
GNU G++17
850 ms
2.6 MB
Accepted