Cod sursa(job #2806998)

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

ifstream fin("ssm.in");
ofstream fout("ssm.out");
vector<int> starts;

int main() {
	int n, value, max_sum = INT_MIN, 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;
			starts.push_back(i);
		}
		current_sum = max(current_sum + value, value);
		if (current_sum > max_sum) {
			end_pos = i;
		}
		max_sum = max(current_sum, max_sum);
	}
	for (int i = 0; i < starts.size() && starts[i] <= end_pos; ++i) {
		start_pos = starts[i];
	}
	fout << max_sum << ' ' << start_pos << ' ' << end_pos;
	return 0;
}