Cod sursa(job #2704342)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 10 februarie 2021 12:53:17
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <algorithm>

using namespace std;

const int N = 2000;

struct P {
    int t;
    int p;
    bool operator<(const P& alt) const {
        return t < alt.t;
    }
} v[N];

int main() {
    ifstream in("carnati.in");
    ofstream out("carnati.out");

    int n, c, s, d, maxst, maxdr, rez = 0;
    in >> n >> c;
    for (int i = 0; i < n; ++i)
        in >> v[i].t >> v[i].p;
    sort(v, v + n);
    for (int i = 0; i < n; ++i) {
        //pretul curent v[i].p
        s = 0;
        maxst = 0;
        for (int j = i - 1; j >= 0; --j) {
            if (v[j].p >= v[i].p)
                s += v[i].p;
            d = (v[i].t - v[j].t) * c;
            maxst = max(maxst, s - d);
        }

        s = 0;
        maxdr = 0;
        for (int j = i; j < n; ++j) {
            if (v[j].p >= v[i].p)
                s += v[i].p;
            d = (v[j].t - v[i].t + 1) * c;
            maxdr = max(maxdr, s - d);
        }

        rez = max(rez, maxst + maxdr);
    }
    out << rez;

    in.close();
    out.close();
    return 0;
}