Cod sursa(job #2653004)

Utilizator teofilotopeniTeofil teofilotopeni Data 26 septembrie 2020 16:29:06
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream in("ssm.in");
    ofstream out("ssm.out");

    int n, i, x, t;
	in >> n >> x;
	int sumaMax = x, firstMax = 1, lastMax = 1, sumaAct = x, firstAct = 1;
	if (x < 0)
    {
        sumaAct = 0;
        firstAct = 2;
    }
    for (i = 2; i <= n; i++)
    {
        in >> x;
        if (sumaMax < 0)
        {
            sumaAct = x;
            if (sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
            sumaAct = 0;
        }
        if (sumaAct + x >= 0)
        {
            if (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;

}