Source Code
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;


int n, k , r, a[100000];
int main(){
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
	cin >> n >> k >> r;
	long long sum = 0;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		sum += a[i];
	}
	int l = 0, r = n-1;
	long long x = sum/r;
	long long y = 0;
	if (k > sum)
	{
		cout << 0 ;
		return 0;
	}
	if (x > k)
	{
		cout << -1 ;
		return 0;
	}
	while (x+y <= k && l < r)
	 {
	 	
	 	if (a[l] <= a[r])
	 	{	
	 		x -= a[l]/r;
	 		y += a[l];
	 		l++;
	 	}else{
	 		y += a[r];
	 		x -= a[r]/r;
	 		r--;
	 	}
	 } 
	 cout << r-l + 1;


	return 0;

}
Copy
Mohannad and Calories microwave6
GNU G++17
21 ms
824 KB
Wrong Answer