Cod sursa(job #2707538)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 17 februarie 2021 11:59:14
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#pragma GCC optimize("Ofast")

using namespace std;

typedef long long ll;
const int NMAX = 5e4 + 5;
int a[NMAX+1];

int main()
{
    ifstream fin("secv2.in");
    ofstream fout("secv2.out");
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int n, k, stop = 0, start = 0, ind = 0;
    ll minx = 1000000000000000LL;
    ll ans = -1000000000000000LL;
    fin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        fin >> a[i];
        a[i] += a[i-1];
        if (i < k);
        else
        {
            if (a[i-k] < minx)
            {
                minx = a[i-k];
                ind = i - k;
            }
            if (a[i] - minx > ans)
            {
                ans = a[i] - minx;
                start = ind + 1;
                stop = i;
            }
        }
    }
    fout << start << " " << stop << " " << ans;

    return 0;
}