Cod sursa(job #2409124)

Utilizator bluestorm57Vasile T bluestorm57 Data 18 aprilie 2019 18:04:21
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

struct variabila{
    int time, money;
};

typedef long long int ll;
const int Nmax = 2005;
variabila v[Nmax];
int best[Nmax];
int n, price, ans;

bool cmp(const variabila &a, const variabila &b){
    return a.time < b.time;
}

int main(){
    int i,j,x,value;
    f >> n >> price;

    for(i = 1 ; i <= n; i++)
        f >> v[i].time >> v[i].money;

    sort(v + 1, v + n + 1, cmp);

    v[0].money = v[0].time = -Nmax;

    ans = 0;

    for(i = 1 ; i <= n ; i++){
        x = v[i].money;
        for(j = 1 ; j <= n ; j++){
            if(v[j].money >= x){
                value = best[j-1] - (v[j].time - v[j-1].time) * price + x;
                best[j] = max(value, x - price);
            }else{
                value = best[j-1] - (v[j].time - v[j-1].time) * price;
                best[j] = max(value, -price);
            }

            ans = max(ans, best[j]);
        }
    }

    g << ans;
    return 0;
}