Cod sursa(job #1755767)

Utilizator preda.andreiPreda Andrei preda.andrei Data 11 septembrie 2016 00:17:54
Problema Carnati Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <iostream>
#include <fstream>

using namespace std;

#define NMAX 2001

int timp[NMAX];
int pret[NMAX];

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

    int n, salariu;
    fin >> n >> salariu;

    for (int i = 1; i <= n; ++i) {
        fin >> timp[i] >> pret[i];
    }

    int profitMax = 0;

    for (int i = 1; i <= n; ++i) {
        int profit = 0;
        int start = 0;

        if (pret[i] <= salariu)
            continue;

        for (int j = 1; j <= n; ++j) {
            int p = profit + ((pret[j] >= pret[i]) ? pret[i] : 0);

            if (start == 0) {
                start = j;
                p -= salariu;
            }
            else {
                p -= salariu * (timp[j] - timp[j - 1]);
            }

            if (p > 0) {
                profit = p;
                profitMax = max(profitMax, profit);
            }
            else if (pret[j] >= pret[i]) {
                profit = pret[i] - salariu;
                start = j;
            }
            else {
                profit = 0;
                start = 0;
            }
        }
    }

    fout << profitMax;
    return 0;
}