Cod sursa(job #2447315)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 12 august 2019 20:34:29
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

int x, n, length{0}, last, first;
long long s{0}, smax{-900000000000};

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> x;

        if(s + x < x)
        {
            length = 1;
            s = x;

            if(s > smax){
                smax = s;
                last = i;
                first = i - length + 1;
             }
        }
        else
        {
             s += x;
             length++;

             if(s > smax){
                smax = s;
                last = i;
                first = i - length + 1;
             }
        }
    }

    fout<<smax<<" "<<first<<" "<<last;


}