Cod sursa(job #1383481)

Utilizator ThomasFMI Suditu Thomas Thomas Data 10 martie 2015 11:58:23
Problema Secventa 2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <fstream>
using namespace std;

#define NMax 50005
#define inf 2100000000

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

int n,k,best[NMax],pr[NMax],mx=-inf,p;

int main()
{
    int i,a;

    f>>n>>k;

    best[0]=-inf;

    for(i=1;i<=n;i++)
    {
        f>>a;
        if(best[i-1]<0) {pr[i]=i;best[i]=a;}
        else {pr[i]=pr[i-1];best[i]=best[i-1]+a;}
        if(best[i]==mx) {if(i-pr[i]<p-pr[p]) p=i;}
        else if(best[i]>mx && i-pr[i]+1>=k) {mx=best[i];p=i;}
    }

    g<<pr[p]<<" "<<p<<" "<<mx<<"\n";

    f.close();
    g.close();
    return 0;
}