Cod sursa(job #2767895)

Utilizator Andrei21AAnea Andrei Andrei21A Data 8 august 2021 14:19:11
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a[6000004],best[6000004],bestSum, n;

void Citire()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i];
}

void Rezolvare()
{
    int pozi,pozf,ok = 0;
    bestSum = a[1];
    pozf = 1;
for (int i = 1; i <= n; ++ i) {
    best[i] = a[i];
    pozi = i;
    if (best[i] < best[i-1] + a[i]){
        best[i] = best[i-1] + a[i];
        if(ok == 0){
                pozi = i;
                ok = 1;
        }
    }
        else ok = 0;
    if (bestSum < best[i]){
        bestSum = best[i];
        pozf = i;
    }

}
fout << bestSum << " " << pozi << " " << pozf;
}

int main()
{
    Citire();
    Rezolvare();
    return 0;
}