Source Code
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    ll k;
    cin>>n>>k;

    vector<ll>a(n);
    for(auto &x:a) cin>>x;

    ll roundsCount=0;
    ll fullRound=accumulate(a.begin(),a.end(),0ll);
    ll l=0,r=2e9;

    while(l<=r){
        ll mid=l+(r-l)/2;
        if((mid*(mid+1)/2)<=k/fullRound){
            roundsCount=mid;
            l=mid+1;
        } else {
            r=mid-1;
        }
    }

    ll ans=roundsCount*n;
    k-=roundsCount*(roundsCount+1)/2*fullRound;

    if(roundsCount%2){
        for(int i=n-1;i>=0;i--){
            if((roundsCount+1)*a[i]>k) break;
            ans++;
            k-=(roundsCount+1)*a[i];
        }
    } else {
        for(int i=0;i<n;i++){
            if((roundsCount+1)*a[i]>k) break;
            ans++;
            k-=(roundsCount+1)*a[i];
        }
    }

    cout<<ans;
}
Copy
Saqqa 26/40 YazanIstatiyeh
GNU G++17
14 ms
1.8 MB
Accepted