Source Code
#include <iostream>
using namespace std;
int main()
{
	long long n, k;
	cin >> n >> k;
	int* initial = new int[n];
	unsigned long long sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> initial[i];
		sum += initial[i];
	}

	int i = 0;
	unsigned long long s = sum;
	int increment = 1;
	unsigned long long climbed = 0;
	int factor = 1;
	while (k >= s)
	{
		climbed += n;
		k -= s;
		factor++;
		s = sum * factor;
		increment *= -1;
	}
	if (increment == -1)
		i = n - 1;
	while (k >= factor * initial[i])
	{
		k -= (factor * initial[i]);
		climbed++;
		i += increment;
		if (i == n)
		{
			i--;
			increment = -1;
			factor++;
		}
		else if (i == -1)
		{
			i++;
			increment = 1;
			factor++;
		}
	}
	cout << climbed;
	delete[] initial;
}
Copy
Saqqa 26/40 Heromnxpw0
GNU G++17
846 ms
1.3 MB
Accepted