Cod sursa(job #2444591)

Utilizator ShayTeodor Matei Shay Data 31 iulie 2019 19:36:24
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

inline int read() {
	int n = 0;
	bool neg = false;
	char c = getchar_unlocked();
	if (c == '-') {
		neg = true;
	}
	while (!('0' <= c && c <= '9')) {
		c = getchar_unlocked();
	}

	while ('0' <= c && c <= '9') {
		n = (n << 3) + (n << 1) + (c - '0');
		c = getchar_unlocked();
	}

	if (neg) {
		return n *= -1;
	}

	return n;
}

int main() {
	freopen("ssm.in", "r", stdin);
	freopen("ssm.out", "w", stdout);
	int n;
	n = read();

	int dp, init_pos, starting_pos, last_pos, sum = 0, max_sum = INT_MIN;
	for (int i = 0 ; i < n ; ++i) {
		dp = read();
		if (sum >= 0) {
			sum += dp;
		} else {
			sum = dp;
			init_pos = i;
		}

		if (max_sum < sum) {
			max_sum = sum;
			starting_pos = init_pos;
			last_pos = i;
		}
	}

	printf("%d %d %d\n", max_sum, starting_pos + 1, last_pos + 1);
	return 0;
}