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 f first
#define s second
using namespace std;
typedef long long ll;
const int N = 1e5+5;
ll n, k, ans;
pair<ll, ll> p[N];
bool cmp(pair<ll, ll> a, pair<ll, ll> b)
{
return a.s < b.s;
}
ll sum(int l, int r)
{
vector<ll> vec;
ll ret = 0;
for(int i=l; i<=r; i++)
{
vec.push_back(p[i].f);
}
sort(vec.rbegin(), vec.rend());
for(int i=0; i<k; i++) ret += vec[i];
return ret;
}
void solve(int l, int r, int s, int e)
{
if(l > r) return ;
ll id = -1, m = -1e18;
ll mid = (l+r)>>1;
for(ll i=max(mid+k-1, (ll)s); i<=e; i++)
{
if(sum(mid, i) - (p[i].s-p[mid].s)*2LL > m)
m = sum(mid, i) - (p[i].s-p[mid].s)*2LL, id = i;
}assert(id != -1);
solve(l, mid-1, s, id);
solve(mid+1, r, id, e);
ans = max(ans, m);
}
int main()
{
cin >> n >> k;
for(int i=0; i<n; i++) cin >> p[i].f >> p[i].s;
sort(p, p+n, cmp);
solve(0, n-k+1, 0, n-1);
cout << ans << 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... |