Cod sursa(job #2767913)

Utilizator Andrei21AAnea Andrei Andrei21A Data 8 august 2021 15:14:32
Problema Subsecventa de suma maxima Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 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];
    if (best[i] < best[i-1] + a[i]){
        best[i] = best[i-1] + a[i];
        if(ok == 0){
                pozi = i - 1;
                ok = 1;
        }
    }
        else {
                ok = 0;
                pozi = i;
            }
    if (bestSum < best[i]){
        bestSum = best[i];
        pozf = i;
    }

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

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