This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
int nbElem, nbVoulus;
struct Container {
priority_queue<int, vector<int>, greater<int>> pq;
int sum = 0;
Container() {}
void add(int x) {
pq.push(x);
sum += x;
while ((int)pq.size() > nbVoulus - 2) {
sum -= pq.top();
pq.pop();
}
}
};
const int INF = 1e18;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> nbElem >> nbVoulus;
vector<pair<int, int>> arr(nbElem);
for (auto &[V, C] : arr)
cin >> V >> C;
sort(arr.begin(), arr.end(), [&](auto l, auto r) {
return pair(l.second, l.first) < pair(r.second, r.first);
});
int sol = -INF;
auto Solve = [&](auto solve, int deb, int fin, int optL, int optR) {
if (deb == fin)
return;
int mid = (deb + fin) / 2;
Container container;
for (int i = mid - 1; i >= optR; --i)
container.add(arr[i].first);
int opt = -1;
int dp = -INF;
for (int i = min(optR, mid) - 1; i >= optL; --i) {
if ((int)container.pq.size() == nbVoulus - 2) {
int v = arr[mid].first + arr[i].first -
2 * (arr[mid].second - arr[i].second) + container.sum;
if (v > dp)
opt = i, dp = v;
}
container.add(arr[i].first);
}
sol = max(sol, dp);
solve(solve, deb, mid, optL, opt + 1);
solve(solve, mid + 1, fin, opt, optR);
};
Solve(Solve, nbVoulus - 1, nbElem, 0, nbElem);
cout << sol << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |