Cod sursa(job #3312277)

Utilizator batasAndrei Batis batas Data 27 septembrie 2025 12:34:13
Problema Secventa 2 Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("secv2.in");
ofstream fout("secv2.out");

const int DIM = 5e4 + 1;
long long n, k;
long long cnt, maxEnd, ans;
int ip, is;
int a[DIM];

int main()
{
    fin >> n >> k;

    for (int i = 1; i <= n; ++i)
        fin >> a[i];

    ip = is = cnt = 0;

    for (int i = 1; i <= n; ++i)
    {
        maxEnd += a[i];
        cnt++;

        if (maxEnd >= 0)
        {
            if (cnt >= k && maxEnd > ans)
            {
                ans = maxEnd;
                ip = i - cnt + 1;
                is = i;
            }
        }
        else
        {
            maxEnd = 0;
            cnt = 0;
        }
    }

    fout << ip << ' ' << is << ' ' << ans;

    return 0;
}