Cod sursa(job #1219018)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 13 august 2014 10:20:12
Problema Subsecventa de suma maxima Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <iostream>
#include <stdio.h>
using namespace std;

int n,sum,a[6000005];

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