Cod sursa(job #2540926)

Utilizator Ionut10Floristean Ioan Ionut10 Data 7 februarie 2020 20:47:02
Problema Secventa 2 Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>

using namespace std;


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


long long n,lg,smax,in,out,s,x,k,i;
int main()
{
    fin>>n>>k;
    smax = -100000000000;
    for(i = 1;i <= n;i++)
    {
        fin>>x;
        if(x > 0)
        {
            s += x;
            lg++;
        }
        else
        {
            if(s > smax && lg >= k)
            {
                smax = s;
                in = i - lg;
                out = i - 1;
            }

                if(s + x < 0)
                {
                    s = 0;
                    lg = 0;
                }
                else
                {
                    s = s + x;
                    lg++;
                }
        }
    }
    if(s > smax && lg >= k)
    {
        smax = s;
        in = i - lg;
        out = i - 1;
    }
    fout<<in<<" "<<out<<" "<<smax;
    return 0;
}