Cod sursa(job #1219020)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 13 august 2014 10:23:23
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 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,finStartPoz = 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,
			finStartPoz = startPoz;
	}
	printf("%d %d %d\n",bestSum,finStartPoz + 1,endPoz + 1);
	return 0;
}