Cod sursa(job #1023763)

Utilizator ibicecIT Zilla ibicec Data 7 noiembrie 2013 18:14:44
Problema Subsecventa de suma maxima Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>
#include <iostream>

int main() {
	std::ifstream inp("ssm.in");
	std::ofstream out("ssm.out");
	
	int n;
	inp >> n;
	
	int *a = new int[n];
	for (int i=0; i<n; i++) {
		inp >> a[i];
	}

	int st = 0, ist = 0, en = 0;

	int *best = new int[n];
	best[0] = a[0];
	
	int max_sum = a[0];
	
	for (int i=1; i<n; i++) {
		if ( a[i] + best[i-1] > a[i] ) {
			best[i] = a[i] + best[i-1];
		} else {
			best[i] = a[i];
			ist = i;
		}
		
		if ( best[i] > max_sum ) {
			max_sum = best[i];
			st = ist;
			en = i;
		}
	}
	


	out << max_sum << " " << st+1 << " " << en+1;
}