Cod sursa(job #1755761)

Utilizator preda.andreiPreda Andrei preda.andrei Data 11 septembrie 2016 00:03:23
Problema Carnati Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 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;

        cerr << "pret nou: " << pret[i] << "\n";

        for (int j = 1; j <= n; ++j) {
            if (start == 0)
                start = j;

            int p = profit + ((pret[j] >= pret[i]) ? pret[i] : 0);
            if (j == start)
                p -= salariu;
            else p -= salariu * (timp[j] - timp[j - 1]);

            if (p > 0) {
                profit = p;
                profitMax = max(profitMax, profit);
                cerr << timp[start] << " " << timp[j] << ": " << profit << "\n";
            }
            else {
                profit = 0;
                start = 0;
            }
        }
    }

    fout << profitMax;
    return 0;
}