Cod sursa(job #2320331)

Utilizator marius0072scarlat marius stefan marius0072 Data 14 ianuarie 2019 17:33:19
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include<fstream>
#include<vector>

std::ifstream f("secv2.in");
std::ofstream g("secv2.out");

int n, k, ok = 1, st_final, dr_final, st, dr;
long long v[50001], maxim = 0, maxim_final = 0,i;

int main()
{
	f>>n>>k;
	for(i=0;i<n;i++)
	{
		f>>v[i];
		if(i<k)
			maxim_final+=v[i];
	}
	dr_final = k - 1;
    st_final = 0;
    st = dr = 0;
    maxim = v[0];
	for(i=1;i<n;i++)
	{
		if (v[i] > maxim + v[i])
        {
            maxim = v[i];
            st = dr = i;
        }
        else
        {
            maxim = maxim + v[i];
            ++dr;
        }
        if (dr - st + 1 >= k)
        {
            if (maxim > maxim_final)
            {
                maxim_final = maxim;
                st_final = st;
                dr_final = dr;
            }
        }
	}
	g<< st_final + 1 << " " << dr_final + 1 << " " << maxim_final;
}