Cod sursa(job #2651377)

Utilizator teofilotopeniTeofil teofilotopeni Data 22 septembrie 2020 14:52:31
Problema Subsecventa de suma maxima Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    ifstream in("ssm.in");
    ofstream out("ssm.out");
    int n, i, x, antMax = 0, totalMax = 0, first = 0, last = 0;
    in >> n;
    for (i = 1; i <= n; i++)
    {
        in >> x;
        if (x > 0)
        {
            //cout << "A crescut de la " << antMax << " cu: " << x << endl;
            antMax += x;
        }
        else
        {
            if (antMax + x > 0)
            {
                //cout << "A crescut de la " << antMax << " cu: " << x << endl;
                antMax += x;
            }
            else
            {
                //cout << "S-a restabilit la: " << i << endl;
                first = i + 1;
                antMax = 0;
            }
        }
        if (antMax > totalMax)
        {
            totalMax = antMax;
            last = i;
        }
    }
    out << totalMax << " " << first << " " << last;
}