Cod sursa(job #1370154)

Utilizator crucerucalinCalin-Cristian Cruceru crucerucalin Data 3 martie 2015 13:13:17
Problema Secventa 2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <iostream>
#include <vector>


int main()
{
    std::ifstream fin("secv2.in");
    std::ofstream fout("secv2.out");

    int N, K;
    fin >> N >> K;

    std::vector<int> v(N);
    for (auto &i : v)
        fin >> i;


    int start_idx = 0, end_idx = 0;
    int current_max = v[0];
    int max = current_max;
    int max_start, max_end;

    for (auto i = 1; i < N; ++i) {
        while (end_idx - start_idx >= K && v[start_idx] <= 0) {
            current_max -= v[start_idx];
            start_idx++;
        }

        current_max += v[i];
        ++end_idx;

        if (current_max > max) {
            max = current_max;
            max_start = start_idx;
            max_end = end_idx;
        }
    }

    fout << max_start + 1 << " " << max_end + 1 << " " << max << "\n";
    return 0;
}