Cod sursa(job #2015972)

Utilizator DianaPopDiana Pop DianaPop Data 28 august 2017 12:35:15
Problema Secventa 2 Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#include <stdlib.h>

int n,x,i,p1,p2;
int t,k,s,smax;
int main(){

    freopen( "secv2.in" , "r" , stdin );
    freopen( "secv2.out" , "w" , stdout );
    scanf("%d %d %d", &n ,&k ,&x );

    s = x;
    smax = x;
    p1 = 1;
    p2 = 1;
    t = 1;

    if( k == n )
        p1 = 1;
        p2 = n;
        smax = x;

    for( i = 2 ; i <= n ; i++ ){
        scanf( "%d" , &x );
        if( k == n ){
            smax = smax + x;
        }else{
            if( s + x < x ){
                s = x;
                t = i;
            }else{
                s = s + x;
            }

            if( s > smax && i - p1 + 1 >= k ){
                    smax = s;
                    p2 = i;
                    p1 = t;
            }
        }
    }

    printf( "%d %d %d" , p1 , p2 , smax );

    return 0;
}