Cod sursa(job #2288104)

Utilizator DariusDCDarius Capolna DariusDC Data 22 noiembrie 2018 20:56:57
Problema Subsecventa de suma maxima Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NLIM = 6000002;

int a[NLIM], best[NLIM];
int n;

int main()
{
    fin >>n;
    best[0] = 0;
    int bestsuma = a[1];
    int pozi;
    int pozj;
    for (int i=1;i<=n;i++)
    {
        fin >>a[i];
        if (best[i] < best[i-1] + a[i])
            best[i] = best[i-1] + a[i];
        else
            best[i] = a[i];
        if (best[i] > bestsuma)
            {
                bestsuma = best[i];
                pozj = i;
            }
    }
    short ok = 0;
    for (int i = pozj;i>=1 && ok==0;i--)
    {
        if (best[i] <= 0)
        {
            pozi = i+1;
            ok = 1;
        }

    }
    fout << bestsuma << " " << pozi << " " << pozj;
    return 0;
}