Submission #1127532

#TimeUsernameProblemLanguageResultExecution timeMemory
1127532Zero_OPCake 3 (JOI19_cake3)C++20
0 / 100
2 ms3400 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAX = 2e5 + 5;

int N, K;
struct cake{
    ll V, C;
    cake() : V(0), C(0) {}
    cake(int V, int C) : V(V), C(C) {}
    bool operator < (const cake& o){
        return make_pair(C, -V) < make_pair(o.C, -o.V);
    }

    friend istream& operator >> (istream& in, cake& c){
        return in >> c.V >> c.C;
    }
} cakes[MAX];

struct F{
    int keep;
    ll sum;
    priority_queue<ll, vector<ll>, greater<ll>> pq;

    F(int k) : pq(), keep(k), sum(0) {}

    void insert(ll x){
        if((int)pq.size() < keep){
            sum += x;
            pq.push(x);
        } else{
            ll mn = pq.top();
            if(mn < x){
                pq.pop();
                sum -= mn;
                sum += x;
                pq.push(x);
            }
        }
    }

    ll get(){
        return sum;
    }
};

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    cin >> N >> K;
    for(int i = 1; i <= N; ++i){
        cin >> cakes[i];
    }

    sort(cakes + 1, cakes + 1 + N);

    ll ans = 0;
    for(int i = 1; i + K - 1 <= N; ++i){
        F max_sums(K - 2);
        for(int j = i + 1; j <= i + K - 2; ++j){
            max_sums.insert(cakes[j].V);
        }

        for(int j = i + K - 1; j <= N; ++j){
            ll earn = max_sums.get() + cakes[i].V + cakes[j].V;
            ll cost = 2LL * (cakes[j].C - cakes[i].C);
            ans = max(ans, earn - cost);
            max_sums.insert(cakes[j].V);
        }
    }

    cout << ans << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...