# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
105017 | YottaByte | Cake 3 (JOI19_cake3) | C++14 | 4 ms | 384 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 int long long
#define fr first
#define sc second
#define pii pair < int, int >
const int N = 4001;
int n, k;
long long ans = -1e18;
pii a[N];
set < int > s;
bool cmp(pii a, pii b) {
if(a.sc < b.sc) return true;
else if(a.sc == b.sc) return (a.fr < b.fr);
else return false;
}
main() {
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i].fr >> a[i].sc;
sort(a + 1, a + n + 1, &cmp);
long long res = 0;
for(int i = 1; i <= n; i++) {
res = a[i].fr;
while(s.size()) {
s.erase(s.begin());
}
for(int j = i + 1; j <= n; j++) {
res += a[j].fr;
if(s.size() > k - 2) {
res -= *s.begin();
s.erase(s.begin());
}
if(s.size() == k - 2)
ans = max(res - 2ll * (a[j].sc - a[i].sc), ans);
s.insert( a[j].fr );
}
}
cout << ans << endl;
}
/***
5 3
2 1
4 2
6 4
8 8
10 16
***/
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... |