Cod sursa(job #2928120)

Utilizator PatruMihaiPatru Mihai PatruMihai Data 22 octombrie 2022 11:19:22
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("carnati.in");
ofstream fout("carnati.out");

const int LMAX = 2007;

struct client
{
    int t, p;
};

bool cmp(client a, client b)
{
    if (a.t != b.t)
        return a.t < b.t;
    return a.p < b.p;
}

int main()
{
    int n, c;
    fin >> n >> c;

    vector<client> v(n);

    for (int i = 0; i < n; i++)
        fin >> v[i].t >> v[i].p;
    sort(v.begin(), v.end(), cmp);

    int ans = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        int prof = INT_MIN;

        for (int j = 0; j < n; j++)
        {
            if(j >= 1)
            sum -= (v[j].t - v[j - 1].t) * c;

            if (sum < 0)
                sum = 0;
            if (v[j].p >= v[i].p)
                sum += v[i].p;
            prof = max(prof, sum);
        }
        ans = max(prof - c, ans);
    }

    fout << ans;


    return 0;
}