Cod sursa(job #2853182)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 19 februarie 2022 23:33:11
Problema Subsecventa de suma maxima Scor 100
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 n, nr, smax, bestprec, stprec, stmax, drmax;
 
int main()
{
    fin >> n >> nr;
    smax = bestprec = nr;
    stprec = stmax = drmax = 1;
 
    for(int i = 2; i <= n; i++)
    {
        fin >> nr;
 
        int besti, sti;
 
        if(bestprec < 0)
        {
            besti = nr;
            sti = i;
        }
        else
        {
            besti = bestprec + nr;
            sti = stprec;
        }
 
        if(besti > smax)
        {
            smax = besti;
            stmax = sti;
            drmax = i;
        }
        bestprec = besti;
        stprec = sti;
    }
    fout << smax << ' ' << stmax << ' ' << drmax;
    return 0;
}