# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
921967 | boris_mihov | Cake 3 (JOI19_cake3) | C++17 | 4081 ms | 8044 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 <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 200000 + 10;
const llong INF = 1e18;
int n, k;
int v[MAXN];
int c[MAXN];
int opt[MAXN];
std::pair <int,int> toSort[MAXN];
std::priority_queue <int> pq;
void solve()
{
std::sort(toSort + 1, toSort + 1 + n);
for (int i = 1 ; i <= n ; ++i)
{
c[i] = toSort[i].first;
v[i] = toSort[i].second;
}
k -= 2;
llong ans = -INF;
for (int i = 1 ; i <= n ; ++i)
{
llong sum = 0;
llong currAns = -INF;
while (pq.size()) pq.pop();
opt[i] = n + 1;
for (int j = i + 1 ; j <= n ; ++j)
{
if (pq.size() == k)
{
llong curr = sum + v[i] + v[j] - 2 * (c[j] - c[i]);
if (currAns < curr)
{
currAns = curr;
opt[i] = j;
}
}
sum += v[j];
pq.push(-v[j]);
if (pq.size() > k)
{
sum += pq.top();
pq.pop();
}
}
ans = std::max(ans, currAns);
}
std::cout << ans << '\n';
}
void input()
{
std::cin >> n >> k;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> toSort[i].second >> toSort[i].first;
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
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... |