Cod sursa(job #2652646)

Utilizator teofilotopeniTeofil teofilotopeni Data 25 septembrie 2020 13:29:48
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream in("ssm.in");
    ofstream out("ssm.out");
    int n, i, x, t;
    int sumaMax = 0, firstMax = 1, lastMax = 1, sumaAct = 0, firstAct = 1;
    in >> n;
    for (i = 1; i <= n; i++)
    {
        in >> x;
        if (sumaAct + x >= 0)
        {
            if (x < 0 && sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
            sumaAct += x;
            if (sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
        }
        else
        {
            sumaAct = 0;
            firstAct = i + 1;
        }
    }
    out << sumaMax << " " << firstMax << " " << lastMax;
    return 0;
}