Cod sursa(job #2806975)

Utilizator george_buzasGeorge Buzas george_buzas Data 23 noiembrie 2021 11:09:56
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
using namespace std;

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 
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> 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);
	}
	cout << max_sum << ' ' << start_pos << ' ' << end_pos;
	return 0;
}