Cod sursa(job #1150909)

Utilizator cristinamateiCristina Matei cristinamatei Data 23 martie 2014 18:07:00
Problema Secventa 2 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>

using namespace std;

int v[50001], sum[50001];

int main()
{
    ifstream in("secv2.in");
    ofstream out("secv2.out");
    int n, k, i, sc = 0, stc, smax, stmax = 1, drmax, pozmin = 1, pozmax;
    in >> n >> k;
    in >> v[1];
    sum[1] = v[1];
    for ( i = 2; i <= n; i++ )
    {
        in >> v[i];
        sum[i] = sum[i-1] + v[i];
    }
    smax = sum[k];
    stc = sum[k];
    pozmax = k;
    drmax = k;
    for ( i = k; i <= n; i++ )
    {
        if ( sum[i] - sum[i-k] > stc + v[i] )
        {
            stc = sum[i] - sum[i-k];
            pozmin = i-k+1;
            pozmax = i;
        }
        else
        {
            stc+=v[i];
            pozmax = i;
        }
        if ( stc > stmax )
        {
            stmax = pozmin;
            drmax = pozmax;
            smax = stc;
        }
    }
    out << stmax<<' '<< drmax << ' '<< smax;
    return 0;
}