This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i = 0; i < (n); ++i)
#define FOO(i,a,b) for(int i = (a); i <= (b); ++i)
#define AI(x) begin(x),end(x)
template<class I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;}
template<class I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;}
#ifdef OWO
#define debug(args...) LKJ("[ " + string(#args) + " ]", args)
void LKJ(){ cerr<<endl;}
template<class I, class...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);}
template<class I> void DE(I a, I b){ while(a < b) cerr<<*a<<" \n"[next(a) == b], ++a;}
#else
#define debug(...) 0
#define DE(...) 0
#endif
const int N = 2e5 + 225;
int n, m;
struct cake{
ll v, c;
cake(ll v = 0, ll c = 0):
v(v), c(c){}
bool operator<(const cake &a){
return c < a.c;
}
} C[N];
ll ans = 0;
typedef priority_queue<ll, vector<ll>, greater<ll>> minheap;
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
FOR(i,n){
ll v, c;
cin >> v >> c;
C[i] = cake(v, c);
}
sort(C, C + n);
minheap pq;
FOR(i,n){
//if(i + m > n) break;
ll sum = C[i].v;
minheap().swap(pq);
for(int j = i + 1; j < n; ++j){
if(pq.size() == m - 1){
sum -= pq.top();
pq.pop();
}
pq.push(C[j].v);
sum += C[j].v;
if(pq.size() == m - 1){
chmax(ans, sum - 2 * (C[j].c - C[i].c));
}
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int32_t main()':
cake3.cpp:51:17: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if(pq.size() == m - 1){
| ~~~~~~~~~~^~~~~~~~
cake3.cpp:57:17: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | if(pq.size() == m - 1){
| ~~~~~~~~~~^~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |