HTML & JS
#include"bits/stdc++.h"
using namespace std;

#define ll  long long int
#define sp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)


void solve(vector<ll>&v , ll k)
{
    ll n = v.size();
    vector<ll>dp(n,0);
    dp[0] = v[0];
    for(ll i=1;i<n;i++)
     dp[i] = v[i]+dp[i-1];
 
 
    ll ans = (long long int)INT_MIN;
    ll st = (long long int)INT_MAX;
    ll en;
    
    for(ll i=0;i<n;i++)
    {
        ll start = 0;
        ll end = i;
        while(start<=end)
        {
            ll mid = (start+end)/2;
            if(mid == 0)
            {
                if(dp[i]<=k)
                {
                if(dp[i]>ans)
                {
                 ans = dp[i];
                 st = 0;
                 en = i;
                 }
                end = mid-1;
                }
                else
                start = mid+1;
            }
            else
            {
                if((dp[i]-dp[mid-1])<=k)
                {
                    
                    if(dp[i]-dp[mid-1]>ans)
                    {
                        ans = dp[i]-dp[mid-1];
                        if(mid<st)
                        {
                            st = mid;
                            en = i;
                        }
                    }
                    
                    end = mid-1;
                }
                else
                start = mid+1;
            }
        }
    }
    if(ans == INT_MIN)
     cout<<"-1"<<endl;
    else
    cout<<st+1<<" "<<en+1<<" "<<ans<<endl;
 }




int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(ll i=0;i<n;i++)
         cin>>v[i];
         
     solve(v,k);
    }
}

Generated URL:

Output:

1 2 2
1 3 10
-1
Report Bug