Cod sursa(job #2294664)

Utilizator dragostanTantaru Dragos Constantin dragostan Data 2 decembrie 2018 17:41:23
Problema Secventa 2 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin("secv2.in");
ofstream cout("secv2.out");

const int DIM = 50001;
int nr[DIM], s[DIM], d[DIM];
int n, k;

int main()
{
	cin >> n >> k;
	int st = 1, dr = 1, maxi = -DIM * 25000, ind1, ind2, imaxi;
	for (int i = 1; i <= n; ++i)
	{
		cin >> nr[i];
		if (i <= k)
		{
			s[i] = s[i - 1] + nr[i];
		}
		else
		{
			s[i] = s[i - 1] + nr[i];
			s[i] -= nr[i - k];
		}
		if (i >= k)
		{
			if (s[i] > maxi)
			{
				maxi = s[i];
				imaxi = i - k + 1;
			}
		}
	}

	int i1 = imaxi, stemp = 0;
	for (int i = imaxi - 1; i >= 1; --i)
	{
		stemp += nr[i];
		if (maxi + stemp > maxi)
		{
			maxi = maxi + stemp;
			i1 = i;
		}
	}

	stemp = 0;
	int i2 = imaxi + k - 1;
	for (int i = i2 + 1; i <= n; ++i)
	{
		stemp += nr[i];
		if (maxi + stemp > maxi)
		{
			maxi = maxi + stemp;
			i2 = i;
		}
	}
	cout << i1 << ' ' << i2 << ' ' << maxi <<  endl;
	getchar();
	getchar();
	return 0;

}