Cod sursa(job #1386660)

Utilizator Mr.DoomRaul Ignatus Mr.Doom Data 13 martie 2015 10:10:06
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream is("carnati.in");
ofstream os("carnati.out");

vector<int> op;
vector<pair<int, int> > p;
int n, c;
int x, profmax = -1;

int main()
{
    is >> n >> c;
    int a, b;
    op.resize(n + 1);
    p.resize(n + 1);
    for ( int i = 1; i <= n; ++i )
    {
        is >> a >> b;
        p[i] = pair<int, int>(a, b);
    }
    sort(p.begin(), p.end());
    p[0].first = p[0].second = -10;
    for ( int i = 1; i <= n; ++i )
        for ( int j = 1; j <= n; ++j )
        {
            x = p[i].second;
            if ( p[j].second < x )
                x = 0;
            if ( op[j - 1] - (p[j].first - p[j - 1].first) * c + x < x - c )
                op[j] = x - c;
            else
                op[j] = op[j - 1] - (p[j].first - p[j - 1].first) * c + x;
            profmax = max(profmax, op[j]);
        }

    os << profmax;

    is.close();
    os.close();
    return 0;
}