Cod sursa(job #358714)

Utilizator nitica_isabelaNitica Isabela nitica_isabela Data 24 octombrie 2009 10:57:43
Problema Subsecventa de suma maxima Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include<stdio.h>

int n, a[6000001];

void Citire()
{
freopen("ssm.in","r",stdin);

scanf("%d",&n);

for(int	i=1 ; i<=n ; i++)
	scanf("%d", a+i);
}

void Rez()
{
int i, pozmax, lgmax, summax, suma, lg, poz;

summax = a[1]; pozmax = 1 ; lgmax = 1 ;
if (a[1] < 0)
{
	suma = 0 ; poz = 2 ; lg = 0 ;
}
else
{
	suma = a[1] ; poz = 1 ; lg = 1 ;
}

 for( i=2; i<=n; i++)	
 {
	suma += a[i] ; 
	lg++ ;
	if (suma > summax)
	{
		summax = suma ;
		lgmax = lg ;
		pozmax = poz ;
	}		
	if(suma < 0) 
	{
		suma = 0;
		poz = i+1 ;
		lg = 0 ;
	}
 }
 freopen("ssm.out","w",stdout) ;
 printf("%d %d %d\n",summax, pozmax, pozmax + lgmax - 1) ;
}

int main()
{
	Citire();
	Rez();
	return 0 ;
}