Cod sursa(job #2332958)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 31 ianuarie 2019 15:11:55
Problema Carnati Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>

#define NMAX 2005
using namespace std;

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

int v[NMAX], cost[NMAX], dp;

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

    for(int i = 1; i <= n; ++i)
        fin >> v[i] >> cost[i];

    int maxxProf = 0;
    for(int i = 1; i <= n; ++i)
    {
        int poz = 1;
        while(poz <= n)
        {
            if(poz == 1)
            {
                dp = (cost[i] <= cost[1] ? cost[i] : 0) - c;
            }
            else
            {
                int x = (cost[i] <= cost[poz] ? cost[i] : 0) - c * (v[poz] - v[poz - 1]);
                if(dp + x > (cost[i] <= cost[poz] ? cost[i] : 0) - c)
                    dp += x;
                else
                    dp = (cost[i] <= cost[poz] ? cost[i] : 0) - c;
            }
            ++poz;
            maxxProf = max(maxxProf, dp);
        }
    }

    fout << maxxProf << '\n';
    return 0;
}