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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 4e3+10;
int n, m, c[maxn], v[maxn], ccval[maxn];
vector<int> vec[maxn];
void solve() {
cin >> n >> m;
vector<int> cc;
for(int i = 1; i <= n; i++) {
cin >> v[i] >> c[i];
cc.pb(c[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
for(int i = 1; i <= n; i++) {
int x = c[i];
c[i] = upper_bound(all(cc),c[i]) - cc.begin();
ccval[c[i]] = x;
vec[c[i]].pb(v[i]);
}
int ans = -inf;
for(int i = 1; i <= cc.size(); i++) {
multiset<int> st1,st2;
int sm = 0;
for(int j = i; j <= cc.size(); j++) {
for(auto x : vec[j]) {
st2.insert(x);
}
while(st1.size() < m && st2.size()) {
auto it = st2.end();
it--;
int x = *it;
st1.insert(x);
sm+= x;
st2.erase(st2.find(x));
}
while(st1.size() && st2.size()) {
auto it1 = st1.begin();
auto it2 = st2.end(); it2--;
if(*it1 >= *it2) break;
else {
sm+= *it2 - *it1;
int x = *it1;
int y = *it2;
st1.erase(st1.find(x));
st1.insert(y);
st2.erase(st2.find(y));
st2.insert(x);
}
}
if(st1.size() >= m) ans = max(ans, sm - 2*ccval[j] + 2*ccval[i]);
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Compilation message (stderr)
cake3.cpp: In function 'void solve()':
cake3.cpp:39:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 1; i <= cc.size(); i++) {
| ~~^~~~~~~~~~~~
cake3.cpp:42:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int j = i; j <= cc.size(); j++) {
| ~~^~~~~~~~~~~~
cake3.cpp:47:30: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
47 | while(st1.size() < m && st2.size()) {
| ~~~~~~~~~~~^~~
cake3.cpp:72:27: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
72 | if(st1.size() >= m) ans = max(ans, sm - 2*ccval[j] + 2*ccval[i]);
| ~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |