Cod sursa(job #1847535)

Utilizator savigunFeleaga Dragos-George savigun Data 14 ianuarie 2017 18:24:13
Problema Secventa 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <iostream>
#include <fstream>
using namespace std;

struct {
    int s, l = 1;
} best[50001];

int main()
{
    ifstream cin("secv2.in");
    //ofstream cout("secv2.out");
    int n, i, k, nr, pmax = 1, smax = 0;
    cin>>n>>k;
    best[0].s = 0;
    best[0].l = 0;

    for(i = 1; i <= n; ++i) {
        cin>>nr;
        if(nr > nr + best[i-1].s) {
            best[i].s = nr;
        } else {
            best[i].s = nr + best[i-1].s;
            best[i].l = best[i-1].l + 1;

            if (best[i].l >= k && best[i].s > smax) {
                smax = best[i].s;
                pmax = i;
            }
        }
    }

    cout<<pmax - best[pmax].l + 1<<" "<<pmax<<" "<<best[pmax].s;

    return 0;
}