Cod sursa(job #2768105)

Utilizator DragosC1Dragos DragosC1 Data 9 august 2021 14:57:20
Problema Carnati Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <algorithm>
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;
 
bool csort(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
        return 1;
    return 0;
}

void solve() {
    int i, sum, Max, j, nr, st;
    sort(v + 1, v + n + 1, csort);
    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) {
                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;
}