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;
	double x = (double)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 -= (float)a[l]/r;
	 		y += a[l];
	 		l++;
	 	}else{
	 		y += a[r];
	 		x -= (float)a[r]/r;
	 		r--;
	 	}
	 } 
	 cout << r-l + 1 << endl;
	 // cout << x;

	return 0;

}
Copy
Mohannad and Calories microwave6
GNU G++17
30 ms
964 KB
Wrong Answer