Cod sursa(job #2601487)

Utilizator As932Stanciu Andreea As932 Data 14 aprilie 2020 15:57:08
Problema Secventa 2 Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <iostream>
#include <fstream>
#define inf 1e10
using namespace std;
ifstream fin("secv2.in");
ofstream fout("secv2.out");

typedef long long ll;

const int nmax=50005;

int n,k,a[nmax];

void read()
{
    fin>>n>>k;

    for(int i=1;i<=n;i++)
        fin>>a[i];
}

void solve()
{
    int st,dr,ind=1;
    ll sum=0,ans=-inf;

    for(int i=1;i<=n;i++)
    {
        sum+=a[i];

        while((i-ind+1>k) && sum-a[ind]>=sum)
        {
            sum-=a[ind];
            ++ind;
        }

        if(sum>ans && (i-ind+1>=k))
        {
            ans=sum;
            st=ind;
            dr=i;
        }
    }

    //0 -6 2 1 4 -1 3 -5

    fout<<st<<" "<<dr<<" "<<ans;
}

int main()
{
    read();
    solve();

    return 0;
}