Cod sursa(job #903491)

Utilizator doomaSalagean Calin dooma Data 1 martie 2013 21:21:47
Problema Subsecventa de suma maxima Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include<iostream>
#include<fstream>
using namespace std;
ofstream g("ssm.out");
ifstream f("ssm.in");
#define NMAX 6000001

int a[NMAX], best[NMAX] = {0};
int main(){

    int i, n, bestSum, bestPoz = 1;
    f >> n;
    for(i = 1; i <= n; i ++){
        f >> a[i];
    }
    bestSum = a[1];
    for(i = 1; i <= n; i ++){
        best[i] = a[i];
        if(best[i] < best[i-1] + a[i])
            best[i] = best[i-1] + a[i];
        if(bestSum < best[i]){
            bestSum = best[i];
            bestPoz = i;
        }
    }
    cout << bestSum;
    for(i = bestPoz; i > 0 && bestSum; i --){
        if(bestSum == best[i]) bestSum -= a[i];
    }
    g << bestSum << " " << i << " " << bestPoz << "\n";
    f.close();
    g.close();
}