Cod sursa(job #1970191)

Utilizator nicu_serteSerte Nicu nicu_serte Data 19 aprilie 2017 00:09:06
Problema Carnati Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("carnati.in");
ofstream fout("carnati.out");
#define nmax 2005
struct om
{
    int h, p;
}v[nmax];
bool cmpf(om a, om b)
{
    return a.h<b.h;
}
int n, C, pr[nmax];
int main()
{
    int i, j, sol=0;
    om x;
    fin>>n>>C;
    for(i=1; i<=n; i++)
        fin>>v[i].h>>v[i].p;
    fin.close();
    sort(v+1, v+1+n, cmpf);
    for(i=1; i<=n; i++)
    {
        x=v[i];
        for(j=1; j<=n; j++)
        {
            pr[j]=0;
            if(v[j].p>=x.p)
                pr[j]=max(pr[j-1]+x.p-C*(v[j].h-v[j-1].h), x.p-C);
            else pr[j]=max(pr[j-1]-C*(v[j].h-v[j-1].h), 0-C);
            if(pr[j]>sol)
                sol=pr[j];
        }
    }
    fout<<sol<<'\n';
    fout.close();
    return 0;
}