Cod sursa(job #1219345)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 14 august 2014 00:20:19
Problema Secventa 2 Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;

int n,k,a[50005];

int main(){
	freopen("secv2.in","r",stdin);
	freopen("secv2.out","w",stdout);
	scanf("%d%d",&n,&k);
	for(int i = 0; i < n; i++){
		scanf("%d",&a[i]);
	}
	ll max = 0,bestSum = 0;
	int startPoz = 0,endPoz = 0,finStartPoz = 0;
	for(int i = 0; i < k; i++){
		bestSum += a[i];
	}
	for(int i = 1; i < n; i++){
		if(max + (ll)a[i] >= (ll)a[i])
			max += (ll)a[i];
		else
			max = (ll)a[i],
			startPoz = i;
		if(bestSum < max){
			
			if(i - startPoz >= k) {
				bestSum = max;
				endPoz = i,
				finStartPoz = startPoz;
			}
		}
	}
	printf("%d %d %lld\n",finStartPoz + 1,endPoz + 1,bestSum);
	return 0;
}