Cod sursa(job #3137299)

Utilizator Mihai_PopescuMihai Popescu Mihai_Popescu Data 12 iunie 2023 11:39:12
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <climits>
using namespace std;

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

int main()
{
    int n;
    fin >> n;

    int start, st = 1, dr = 0;
    int s = -1, smax = -INT_MAX;

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

        if (s < 0)
        {
            s = 0;
            start = i;
        }
        s += x;
        if (s > smax)
        {
            smax = s;
            st = start;
            dr = i;
        }
        else if (s == smax && i - start < dr - st)
        {
            st = start;
            dr = i;
        }
    }

    fout << smax << ' ' << st << ' ' << dr;
    return 0;
}