Cod sursa(job #664316)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 19 ianuarie 2012 21:58:00
Problema Euro Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>

#define MAX 35000

using namespace std;

int n, t;
long long solution[MAX];
long long sume[MAX];
long long day[MAX];
int negs;

void citire()
{
    ifstream in("euro.in");
    int x;
    long long s = 0;
    in>>n>>t;
    for(int i = 1; i < n; i++)
    {
        in>>x;
        s += x;
        if(s < 0)
        {
            sume[++negs] = s;
            day[negs] = i;
            s = 0;
        }
    }
    in>>x;
    s += x;
    sume[++negs] = s;
    day[negs] = n;
    in.close();
}

void solve()
{
    long long actual = 0;
    for(int i = 1; i <= negs; i++)
    {
        solution[i] = solution[i - 1] + sume[i] * day[i];
        actual = sume[i];
        for(int j = i - 1; j > 0; j--)
        {
            actual += sume[j];
            solution[i] = max(solution[i], solution[j - 1] + actual * day[i]);
        }
        solution[i] -= t;

    }
}

void afisare()
{
    ofstream out("euro.out");
    out<<solution[negs];
    out.close();
}

int main()
{
    citire();
    solve();
    afisare();
    return 0;
}