# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
785666 | SamNguyen | Cake 3 (JOI19_cake3) | C++14 | 1 ms | 340 KiB |
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>
using namespace std;
#define INPFILE "cake3.inp"
#define OUTFILE "cake3.out"
using lli = long long;
template <class T1, class T2>
inline bool maximise(T1 &x, T2 y) {
if (x < y) { x = y; return true; }
return false;
}
template <class T1, class T2>
inline bool minimise(T1 &x, T2 y) {
if (x > y) { x = y; return true; }
return false;
}
const int MAX = 2e5 + 7;
int N, M;
pair<lli, lli> piece[MAX];
inline lli GET_V(int i) { return piece[i].second; }
inline lli GET_C(int i) { return piece[i].first; }
void input() {
cin >> N >> M;
for (int i = 1; i <= N; i++) {
lli v, c; cin >> v >> c;
piece[i] = make_pair(c, v);
}
sort(piece + 1, piece + N + 1);
}
void solve() {
lli ans = 0;
for (int l = 1; l <= N; l++) {
priority_queue<lli, vector<lli>, greater<lli>> pq;
lli sum_v = 0;
for (int r = l; r <= N; r++) {
pq.push(GET_V(r));
sum_v += GET_V(r);
while (int(pq.size()) > M) {
sum_v -= pq.top();
pq.pop();
}
if (int(pq.size()) == M)
maximise(ans, sum_v - 2 * (GET_C(r) - GET_C(l)));
}
}
cout << ans;
}
int main(void) {
if (fopen(INPFILE, "r")) {
freopen(INPFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
input();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |