Cod sursa(job #2926832)

Utilizator rares89_Dumitriu Rares rares89_ Data 18 octombrie 2022 19:31:42
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
ifstream fin("carnati.in");
ofstream fout("carnati.out");

struct pb {
    int t, p;
} v[2005];

bool cmp(pb a, pb b) {
    return a.t < b.t || (a.t == b.t && a.p < b.p);
}

int n, c, dp[2005], maxim = -2e9 - 5;
// dp[i] = profitul maxim al unei subsecvente care se termina la timpul t[i];

int main() {
    fin >> n >> c;
    for(int i = 1; i <= n; i++) {
        fin >> v[i].t >> v[i].p;
    }
    sort(v + 1, v + n + 1, cmp);
    for(int i = 1; i <= n; i++) { // se verifica fiecare p
        int s = 0, profit = -2e9 - 5;
        // calc dinamica (rasp este val maxima din dp)
        for(int j = 1; j <= n; j++) {
            s -= (v[j].t - v[j - 1].t) * c;
            if(s < 0) {
                s = 0;
            }
            if(v[j].p >= v[i].p) {
                s += v[i].p;
            }
            profit = max(profit, s);
        }
        maxim = max(maxim, profit - c);
    }
    fout << maxim;
	return 0;
}