Source Code
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main(){
    int n,k,r;
    scanf("%d%d%d",&n,&k,&r);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    vector<long long> cm(n+1,0);
    for(int i=0;i<n;i++){
        cm[i+1]=cm[i]+a[i];
    }
    long long sum=cm[n];
    if(sum/r > k){
        cout<<-1<<endl;
        return 0;
    }
    long long cur;
    int ans=n+10;
    int i=1,j=0;
    while(j<=n && j+1>=i){
        cur=cm[j]-cm[i-1];
        long long other = sum-cur;
        long long here = other + cur/r;
        if(here > k){
            j++;
        }else{
            ans=min(ans,j-i+1);
            i++;
        }
    }
    cout<<ans<<endl;
    
    return 0;
}
Copy
Mohannad and Calories Saq
GNU G++17
22 ms
1.9 MB
Accepted