Cod sursa(job #1559881)

Utilizator demetriad-dagpagDavid Demetriad demetriad-dagpag Data 31 decembrie 2015 18:18:11
Problema Secventa 2 Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <stdio.h>
#include <stdlib.h>
int v[50001];
int main()
{
    int n,k,s,max,a,aa,b,bb,i;
    freopen("secv2.in","r",stdin);
    freopen("secv2.out","w",stdout);
    scanf("%d%d%d",&n,&k,&v[1]);
    if(k==1){
        s=max=v[1];
        a=aa=1;
        b=bb=1;
    }
    else
    {
        s=v[1];
        aa=1;
        bb=1;
        a=0;
        b=0;
        max=-26000;
    }
    for(i=2; i<=n; i++)
    {
        scanf("%d",&v[i]);
        bb=i;
        if(s>=0){
            s+=v[i];
            if(k<=bb-aa+1 && max<s)
            {
                max=s;
                a=aa;
                b=bb;
            }
        }
        else
        {
            if(k<=bb-aa+1 && max<s)
            {
                max=s;
                a=aa;
                b=bb;
            }
            aa=i;
            s=v[i];
        }
    }
    if(max==-26000){
        s=0;
        for(i=1; i<=k; i++)
            s+=v[i];
        max=s;
        a=1;
        b=k;
        for(; i<=n; i++)
        {
            s=s-v[i-k]+v[i];
            if(max<s){
                max=s;
                a=i-k+1;
                b=i;
            }
        }
    }
    printf("%d %d %d\n",a,b,max);

    return 0;
}