Cod sursa(job #502289)

Utilizator PlayLikeNeverB4George Marcus PlayLikeNeverB4 Data 18 noiembrie 2010 18:58:30
Problema Subsecventa de suma maxima Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <stdio.h>

const int maxn=6000001;
int i,N,ind,A[maxn],S[maxn],st[maxn];

void citire()
{
	scanf("%d",&N);
	for(i=1;i<=N;i++) scanf("%d",&A[i]);
}

void pd()
{
	for(i=1;i<=N;i++)
		if(S[i-1]+A[i]>A[i]) 
		{
			S[i]=S[i-1]+A[i];
			st[i]=st[i-1];
		}
		else 
		{
				S[i]=A[i];
				st[i]=i;
		}
}

void best()
{
	int max=S[1]; ind=1;
	for(i=2;i<=N;i++)
		if(S[i]>max)
		{
			max=S[i];
			ind=i;
		}
}

int main()
{
	freopen("ssm.in","r",stdin);
	freopen("ssm.out","w",stdout);
	citire();
	pd();
	best();
	printf("%d %d %d",S[ind],st[ind],ind);
}