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;
struct ab {
int a, b;
};
int m;
const int MAX_N = 2e5;
const long long INF = 1e18;
ab v[MAX_N];
multiset<int> s;
long long solve( int ll, int lr, int rl, int rr ) {
if ( ll > lr )
return -INF;
int l = (ll + lr) / 2;
rl = max( rl, l + m );
long long sumA = 0, answer = -INF;
int bestR = rr;
s.clear();
for ( int r = l; r < rl; r++ ) {
s.insert( v[r].a );
sumA += v[r].a;
if ( s.size() > m ) {
sumA -= *s.begin();
s.erase( s.begin() );
}
}
for ( int r = rl; r <= rr; r++ ) {
s.insert( v[r].a );
sumA += v[r].a;
if ( s.size() > m ) {
sumA -= *s.begin();
s.erase( s.begin() );
}
if ( s.size() >= m && r >= rl ) {
if ( sumA - 2 * (v[r].b - v[l].b) > answer ) {
answer = sumA - 2 * (v[r].b - v[l].b);
bestR = r;
}
}
}
return max( answer, max( solve( ll, l - 1, rl, bestR ), solve( l + 1, lr, bestR, rr ) ) );
}
int main() {
int n;
cin >> n >> m;
for ( int i = 1; i <= n; i++ )
cin >> v[i].a >> v[i].b;
sort( v + 1, v + 1 + n, []( ab x, ab y ) {
return x.b < y.b;
} );
cout << solve( 1, n, 1, n );
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'long long int solve(int, int, int, int)':
cake3.cpp:28:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
cake3.cpp:37:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
cake3.cpp:42:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
42 | if ( s.size() >= m && r >= rl ) {
| ~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |