Cod sursa(job #2926831)

Utilizator rares89_Dumitriu Rares rares89_ Data 18 octombrie 2022 19:27:04
Problema Carnati Scor 90
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], ans = -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 t[2005] = {0}, k = 0;
        // vectorul de timpi
        for(int j = 1; j <= n; j++) {
            if(v[j].p >= v[i].p) {
                t[++k] = v[j].t;
            }
        }
        // calc dinamica (rasp este val maxima din dp)
        for(int j = 1; j <= k; j++) {
            dp[j] = max(v[i].p - c, dp[j - 1] + v[i].p - c * (t[j] - t[j - 1]));
            ans = max(ans, dp[j]);
        }
    }
    fout << ans;
	return 0;
}