Cod sursa(job #2305824)

Utilizator DariusDCDarius Capolna DariusDC Data 21 decembrie 2018 10:20:53
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#define nmax 6000000
#define infinit (1 << 30)

using namespace std;

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

int n;
int best[nmax];
bool p[nmax];
int a[nmax];
int poz;

int main()
{
    fin >> n;
    for  (int i = 1; i <= n; i++)
        fin >>a[i];
    best[1] = a[1];
    int maxim = -infinit;
    for (int i = 2; i <= n ; i++)
    {
        if (a[i] > a[i] + best[i-1])
        {
            p[i] = true;
            best[i] = a[i];
        }
        else best[i] = best[i-1] + a[i];
        if (best[i] > maxim)
        {
            poz= i;
            maxim = best[i];
        }
    }
    fout << maxim << " ";
    int poz1 = poz;
    while (p[poz1]!=true)
        poz1--;
    fout << poz1 << " " << poz;
    return 0;
}