Cod sursa(job #1686930)

Utilizator KrosomAngelo Barbu Krosom Data 12 aprilie 2016 15:25:45
Problema Subsecventa de suma maxima Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define DIM 6000003

using namespace std;

ifstream f("ssm.in");
ofstream g("ssm.out");

int a[DIM], best[DIM];
int bestmax, poz, n;

void read()
{
    f>>n;
    f>>a[1];
    best[1]=a[1];
    bestmax=best[1];
    for (int i=2; i<=n; i++)
    {
        f>>a[i];
        best[i]=a[i];
        if (best[i] < a[i] + best[i-1])
            best[i]=a[i]+best[i-1];
        if (bestmax<best[i])
        {
            bestmax=best[i];
            poz=i;
        }
    }
    f.close();
}

int main()
{
    read();
    int max1=bestmax;
    int poz1=poz;
    int suma=0;
    while (max1)
    {
        max1-=a[poz1];
        poz1--;
    }
    poz1++;
    for (int i=poz1; i<=poz; i++)
        suma+=a[i];
    g<<suma<<" "<<poz1<<" "<<poz;
    g.close();
    return 0;
}