Cod sursa(job #2810431)

Utilizator george_buzasGeorge Buzas george_buzas Data 29 noiembrie 2021 14:29:50
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <climits>
using namespace std;

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

int main() {
	int n, value, max_sum = INT_MIN, current_sum = 0, start_index = 0, last_starting_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_index = i;
		}
		current_sum = max(current_sum + value, value);
		if (current_sum > max_sum) {
			last_starting_pos = start_index;
			end_pos = i;
		}
		max_sum = max(current_sum, max_sum);
	}
	fout << max_sum << ' ' << last_starting_pos << ' ' << end_pos;
	return 0;
}