Cod sursa(job #2768091)

Utilizator DragosC1Dragos DragosC1 Data 9 august 2021 14:44:44
Problema Carnati Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
using namespace std;

int n, c;

pair<int, int> v[2001];

void read() {
    int i;
    ifstream f("carnati.in");
    f >> n >> c;
    for (i = 1; i <= n; i++)
        f >> v[i].first >> v[i].second;
    f.close();
}

int Maxglobal;

void solve() {
    int i, sum, Max, j, nr, st;
    for (i = 1; i <= n; i++) {
        sum = 0;
        Max = 0;
        st = 1;
        nr = 0;
        for (j = 1; j <= n; j++) {
            nr += (v[j].second >= v[i].second);
            sum = -(v[j].first - v[st].first + 1) * c + nr * v[i].second;
            if (sum > Max) 
                Max = sum;
            if (sum < 0) {
                st = j + 1;
                nr = 0;
            }
        }
        if (Max > Maxglobal)
            Maxglobal = Max;
    }
}

void output() {
    ofstream g("carnati.out");
    g << Maxglobal;
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}