Cod sursa(job #2451321)

Utilizator ShayTeodor Matei Shay Data 26 august 2019 15:16:40
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

const int BUFFER_SIZE = 1 << 17;

char buffer[BUFFER_SIZE];
int pos = BUFFER_SIZE;

inline char next() {
    if (pos == BUFFER_SIZE) {
        fread(buffer, 1, BUFFER_SIZE, stdin);
        pos = 0;
    }

    return buffer[pos++];
}

inline int read() {
    int x = 0, neg = 1;
    char c = next();
    if (c == '-') {
    	neg = -1;
    }

    while (!('0' <= c && c <= '9')) {
        c = next();
    }

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

    return x * neg;
}

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;
		}
	}

	++starting_pos;
	++last_pos;

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