Cod sursa(job #3207257)

Utilizator mdayAyabakti Muhammed Melih mday Data 25 februarie 2024 17:06:39
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>

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

const int N = 6e6, oo = -2e9;

int n, best[N];

int main () {
    fin >> n;

    int x, bestSum = oo, st = 0, dr = 0, maxSt = 0, maxDr = 0;
    fin >> x;
    best[0] = x;
    for(int i = 1; i < n; ++i){
	fin >> x;
	if(x > x + best[i - 1]) {
	    best[i] = x;
	    st = i, dr = i;
	}
	else {
	    best[i] = x + best[i - 1];
	    dr++;
	}
	if(best[i] > bestSum) {
	    bestSum = best[i];
	    maxSt = st, maxDr = dr;
	}
    }

    maxSt++, maxDr++;
    fout << bestSum << ' ' << maxSt << ' ' << maxDr << '\n';

    fin.close();
    fout.close();
    return 0;
}