Cod sursa(job #2432347)

Utilizator Ionut10Floristean Ioan Ionut10 Data 23 iunie 2019 10:26:38
Problema Secventa 2 Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>

using namespace std;


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


int n,lg,smax,in,out,s,x,k,i;
int main()
{
    fin>>n>>k;
    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++;
                }
        }
    }
    fout<<in<<" "<<out<<" "<<smax;
    return 0;
}