Cod sursa(job #2828894)

Utilizator bumblebeeGeorge Bondar bumblebee Data 8 ianuarie 2022 08:54:01
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <climits>
using namespace std;

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

int main() {
	int n, best = 0, start_idx = 1, end_idx = 1, max_best = INT_MIN, value, max_start_idx = 1, max_end_idx = 1;
	fin >> n;
	for (int i = 1; i <= n; ++i) {
		fin >> value;
		if (value + best >= value) {
			best += value;
		} else if (value > best) {
			best = value;
			start_idx = i;
		}
		if (best > max_best) {
			max_best = best;
			max_start_idx = start_idx;
			max_end_idx = i;
		}
	}
	fout << max_best << " " << max_start_idx << " " << max_end_idx;
	return 0;
}

/*
100
1 -7 18 22 27 -38 24 33 -14 30 41 47 -43 -16 -43 -45 -37 -26 -28 -22 -43 30 34 32 -42 28 -13 -35 -7 -14 -24 8 19 1 -37 31 -14 -6 11 33 32 29 -5 0 -33 45 -44 -6 -5 -39 -8 -33 23 -42 18 28 -46 40 -10 38 25 -1 3 10 -15 4 -33 46 -12 -10 -19 29 -12 -11 33 -25 -49 24 8 -10 -14 28 14 -29 -21 -40 17 18 49 -24 -40 28 -16 46 -9 43 45 -29 -15 6
=> 190 3 12
*/