Cod sursa(job #2651745)

Utilizator teofilotopeniTeofil teofilotopeni Data 23 septembrie 2020 15:06:58
Problema Subsecventa de suma maxima Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    ifstream in("ssm.in");
    ofstream out("ssm.out");
    int n, i, x, maxAct = 0, firstAct = 1, last = 1, maxTotal = 0, firstTotal = 1;
    in >> n;
    for (i = 1; i <= n; i++)
    {
        in >> x;
        if (maxAct + x >= 0)
        {
            maxAct += x;
        }
        if (maxAct > maxTotal)
        {
            firstTotal = firstAct;
            maxTotal = maxAct;
            last = i;
        }
        if (maxAct + x < 0)
        {
            firstAct = i + 1;
            maxAct = 0;
        }
    }
    out << maxTotal << " " << firstTotal << " " << last;
}