Cod sursa(job #2806977)

Utilizator george_buzasGeorge Buzas george_buzas Data 23 noiembrie 2021 11:11:35
Problema Subsecventa de suma maxima Scor 85
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

int main() {
	int n, value, max_sum = 0, current_sum = 0, start_pos = 0, end_pos = 0;
	// current_sum - stores the maximum value between the current value and the sum of previously computed sum and current value 
	// max_sum - computes the maximum value between 0 and the largest value out of all current sums 
	fin >> n;
	for (int i = 1; i <= n; ++i) {
		fin >> value;
		if (value > current_sum + value) {
			start_pos = i;
		}
		current_sum = max(current_sum + value, value);
		if (current_sum > max_sum) {
			end_pos = i;
		}
		max_sum = max(current_sum, max_sum);
	}
	fout << max_sum << ' ' << start_pos << ' ' << end_pos;
	return 0;
}