Cod sursa(job #2954760)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 15 decembrie 2022 11:40:38
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin ("carnati.in");
ofstream cout ("carnati.out");

const int N = 2000;

class upsy
{
public:
    int timp, plata;
    bool operator < (const upsy &a)const
    {
        return timp < a.timp;
    }
} a[N + 1];

int n, cost, profit, ups = -1, preta;

int main()
{
    cin >> n >> cost;
    for (int i = 1; i <= n; ++i)
        cin >> a[i].timp >> a[i].plata;
    sort (a + 1, a + n + 1);
    a[0].timp = a[1].timp - 1;
    for (int i = 1; i <= n; ++i)
    {
        int pret = a[i].plata;
        ups = -1;
        for (int j = 1; j <= n; ++j)
        {
            int val = (a[j - 1].timp - a[j].timp) * cost;
            int val1 = -cost;
            if (a[j].plata >= pret)
                val += pret, val1 += pret;
            if (ups + val < val1)
                ups = val1;
            else
                ups += val;
            profit = max (profit, ups);
        }
    }
    cout << profit <<'\n';
    return 0;
}