Cod sursa(job #957338)

Utilizator OpportunityVlad Negura Opportunity Data 4 iunie 2013 21:06:49
Problema Secventa 2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fi("secv2.in");
ofstream fo("secv2.out");

int j,i,n,k,a[50001],s[50001],m=-1<<21,st,fn,semn=1;
string str;

int main()
{
	fi >> n >> k; getline(fi,str);
	getline(fi,str);
	str+=' ';
	j=1;
	for (i=0; i<(int)str.size(); i++)
	{
		if (str[i]==' ') j++,a[j-1]*=semn,semn=1; else
		if (str[i]=='-') semn=-1; else a[j]=a[j]*10+str[i]-'0';

	}	
	
	for (i=1; i<=n; i++)
		s[i]=s[i-1]+a[i];
	
	for (i=0; i<=n-k; i++)
		for (j=i+k; j<=n; j++)
			{
				int sum=s[j]-s[i];
				if (sum>m)
				{
					m=sum;
					st=i+1;
					fn=j;
				}
			}
	
	fo << st << ' ' << fn << ' ' << m;
	
	
	return 0;
}