Cod sursa(job #1510393)

Utilizator metrix007Lungu Ioan Adrian metrix007 Data 24 octombrie 2015 22:01:19
Problema Secventa 2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
#define NMAX 50003
using namespace std;

ifstream in("secv2.in");
ofstream out("secv2.out");

int n,k,a[NMAX],s,smax,l,lmax,imax;
bool ok = true;
int main()
{
    in  >> n >> k;
    for(int i=1;i<=n;i++)
        in >> a[i];
    s = smax = a[1]; imax = 1; l = lmax =1;
    for(int i=2;i<=n;i++)
    {
        if(s>=0 || (s<0 && l<k))
        {
            s = s + a[i];
            l++;

        }
        else
        {
            s = a[i];
            l = 1;
        }
        if((smax<s && l>=k) || (ok==true && l==k))
        {
            ok = false;
         smax = s;
         lmax = l;
         imax = i-l+1;
        }
    }
    out << imax << " " << imax + lmax-1 << " " <<  smax;
    return 0;
}

//13 13
//-24468 -3302 -23557 -1899 -4410 -650 -6467 -9733 -16334 -15731 -7225 -2730 -11983