Cod sursa(job #599577)

Utilizator darrenRares Buhai darren Data 29 iunie 2011 09:52:35
Problema Carnati Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <cstring>
#include <fstream>
#include <algorithm>
#include <vector>

int N, C;
int money[2002], time[2002], pos[2002];
int maxS[2002];
int result;

bool compare(const int& i1, const int& i2)
{
    return time[i1] < time[i2];
}

int main()
{
    std::ifstream fin("carnati.in");
    std::ofstream fout("carnati.out");

    fin >> N >> C;
    for (int i = 1; i <= N; ++i)
    {
        fin >> time[i] >> money[i];
        pos[i] = i;
    }

    std::sort(pos + 1, pos + N + 1, compare);

    for (int i = 1; i <= N; ++i) // daca pretul este i
    {
        maxS[pos[1]] = (money[pos[1]] >= money[i] ? money[i] : 0) - C;
        for (int j = 2; j <= N; ++j)
        {
            int add = (money[pos[j]] >= money[i] ? money[i] : 0);
            maxS[pos[j]] = add - C;
            maxS[pos[j]] = std::max(maxS[pos[j]], maxS[pos[j - 1]] + add - C * (time[pos[j]] - time[pos[j - 1]]));

            result = std::max(result, maxS[pos[j]]);
        }

        result = std::max(result, maxS[pos[1]]);
    }

    fout << result;

    fin.close();
    fout.close();
}