Cod sursa(job #471056)

Utilizator robigiirimias robert robigi Data 16 iulie 2010 19:11:24
Problema Subsecventa de suma maxima Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
// SubsecventaDeSumaMaxima.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include "stdio.h"

FILE *f=fopen("ssm.in", "r");
FILE *g=fopen("ssm.out", "w");


int n, v;
int bst[2][2];


void read()
{
	fscanf(f, "%d", &n);
}


void program()
{
	int ret;
	int max=-1000000000;
	int maxi=0;
	for (int i=1; i<=n; i++)
	{
		fscanf(f, "%d", &v);
		if (bst[0][0]+v>bst[1][0])
		{
			bst[1][1]=bst[0][1];
			bst[1][0]=bst[0][0]+v;
		}
		else
		{
			bst[1][1]=i;
			bst[1][0]=v;
		}
		if (bst[1][0]>max)
		{
			max=bst[1][0];
			maxi=i;
			ret=bst[1][1];
		}

		bst[0][1]=bst[1][1];
		bst[0][0]=bst[1][0];
		bst[1][0]=0;
		bst[1][1]=0;
	}
	fprintf(g, "%d ", max);	
	fprintf(g, "%d %d", ret, maxi);
}
		


int main()
{
	read();
	program();
	return 0;
}