Cod sursa(job #2332951)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 31 ianuarie 2019 14:59:25
Problema Carnati Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>

#define NMAX 2005
using namespace std;

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

int v[NMAX], cost[NMAX];

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 p1 = 1, p2 = 2;
        int prof = (cost[i] <= cost[1] ? cost[i] : 0) - c;
        while(p2 <= n){
            int lasProf = prof;
            prof += (cost[i] <= cost[p2] ? cost[i] : 0) - c * (v[p2] - v[p2 - 1]);
            if(prof < lasProf)
            {
                prof = prof - (cost[i] <= cost[p1] ? cost[i] : 0) + c * (v[p1 + 1] - v[p1]);
                ++p1;
            }

            if(prof > maxxProf)
                maxxProf = prof;
            ++p2;
        }
    }

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