//#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>
#include <bitset>
#include <map>
#include <cassert>
#include <array>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("inputik.txt", "r", stdin);
// freopen("outputik.txt", "w", stdout);
}
vpii kek;
int n, m;
int ans = 0;
int all_ans = -inf*inf;
int cl = 0, cr = -1;
multiset<int> fir, sec;
int get(int l, int r){
while(cr < r){
cr++;
// cout << " + " << kek[cr].second << endl;
sec.insert(kek[cr].second);
ans += kek[cr].second;
if(sec.size() > m-2){
ans -= *sec.begin();
fir.insert(*sec.begin());
sec.erase(sec.begin());
}
}
while(cl > l){
cl--;
// cout << " + " << kek[cl].second << endl;
sec.insert(kek[cl].second);
ans += kek[cl].second;
if(sec.size() > m-2){
ans -= *sec.begin();
fir.insert(*sec.begin());
sec.erase(sec.begin());
}
}
while(cr > r){
auto it = sec.find(kek[cr].second);
// cout << " - " << kek[cr].second << endl;
if(it == sec.end()){
fir.erase(fir.find(kek[cr].second));
} else{
ans -= *it;
sec.erase(it);
if(fir.size()){
sec.insert(*(--fir.end()));
ans += *(--fir.end());
fir.erase(--fir.end());
}
}
cr--;
}
while(cl < l){
// cout << " - " << kek[cl].second << endl;
auto it = sec.find(kek[cl].second);
if(it == sec.end()){
fir.erase(fir.find(kek[cl].second));
} else{
ans -= *it;
sec.erase(it);
if(fir.size()){
sec.insert(*(--fir.end()));
ans += *(--fir.end());
fir.erase(--fir.end());
}
}
cl++;
}
return ans;
}
void calc(int l, int r, int tl, int tr){
if(l > r)
return;
int mid = (l + r)/2;
int res = -inf*inf, pos = tr;
for(int i=max(mid+m-1, tl); i<=tr; i++){
if(res <= get(mid+1, i-1) + kek[i].second + kek[mid].second - (kek[i].first - kek[mid].first)*2){
res = ans + kek[i].second + kek[mid].second - (kek[i].first - kek[mid].first)*2;
pos = i;
}
}
all_ans = max(all_ans, res);
calc(l, mid-1, tl, pos);
calc(mid+1, r, pos, tr);
}
void solve(){
cin >> n >> m;
kek.resize(n);
for(int i=0; i<n; i++){
int a, b;
cin >> a >> b;
kek[i] = make_pair(b, a);
}
sort(all(kek));
calc(0, n-1, 0, n-1);
cout << all_ans << endl;
}
/*
5 3
2 1
4 2
6 4
8 8
10 16
======
8 4
112103441 501365808
659752417 137957977
86280801 257419447
902409188 565237611
965602301 689654312
104535476 646977261
945132881 114821749
198700181 915994879
*/
signed main(){
fast_io();
// srand(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while(q--)
solve();
}
Compilation message
cake3.cpp: In function 'long long int get(long long int, long long int)':
cake3.cpp:65:23: 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]
65 | if(sec.size() > m-2){
| ~~~~~~~~~~~^~~~~
cake3.cpp:76:23: 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]
76 | if(sec.size() > m-2){
| ~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
392 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
392 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
8 ms |
492 KB |
Output is correct |
39 |
Correct |
9 ms |
556 KB |
Output is correct |
40 |
Correct |
5 ms |
492 KB |
Output is correct |
41 |
Correct |
6 ms |
492 KB |
Output is correct |
42 |
Correct |
4 ms |
492 KB |
Output is correct |
43 |
Correct |
5 ms |
492 KB |
Output is correct |
44 |
Correct |
10 ms |
492 KB |
Output is correct |
45 |
Correct |
8 ms |
492 KB |
Output is correct |
46 |
Correct |
8 ms |
492 KB |
Output is correct |
47 |
Correct |
9 ms |
628 KB |
Output is correct |
48 |
Correct |
9 ms |
492 KB |
Output is correct |
49 |
Correct |
9 ms |
492 KB |
Output is correct |
50 |
Correct |
7 ms |
492 KB |
Output is correct |
51 |
Correct |
7 ms |
492 KB |
Output is correct |
52 |
Correct |
6 ms |
492 KB |
Output is correct |
53 |
Correct |
7 ms |
492 KB |
Output is correct |
54 |
Correct |
5 ms |
492 KB |
Output is correct |
55 |
Correct |
4 ms |
492 KB |
Output is correct |
56 |
Correct |
4 ms |
492 KB |
Output is correct |
57 |
Correct |
4 ms |
512 KB |
Output is correct |
58 |
Correct |
4 ms |
492 KB |
Output is correct |
59 |
Correct |
5 ms |
492 KB |
Output is correct |
60 |
Correct |
7 ms |
492 KB |
Output is correct |
61 |
Correct |
5 ms |
492 KB |
Output is correct |
62 |
Correct |
4 ms |
492 KB |
Output is correct |
63 |
Correct |
5 ms |
492 KB |
Output is correct |
64 |
Correct |
5 ms |
492 KB |
Output is correct |
65 |
Correct |
8 ms |
492 KB |
Output is correct |
66 |
Correct |
7 ms |
492 KB |
Output is correct |
67 |
Correct |
8 ms |
492 KB |
Output is correct |
68 |
Correct |
7 ms |
492 KB |
Output is correct |
69 |
Correct |
6 ms |
492 KB |
Output is correct |
70 |
Correct |
6 ms |
464 KB |
Output is correct |
71 |
Correct |
7 ms |
492 KB |
Output is correct |
72 |
Correct |
4 ms |
492 KB |
Output is correct |
73 |
Correct |
5 ms |
492 KB |
Output is correct |
74 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
392 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
8 ms |
492 KB |
Output is correct |
39 |
Correct |
9 ms |
556 KB |
Output is correct |
40 |
Correct |
5 ms |
492 KB |
Output is correct |
41 |
Correct |
6 ms |
492 KB |
Output is correct |
42 |
Correct |
4 ms |
492 KB |
Output is correct |
43 |
Correct |
5 ms |
492 KB |
Output is correct |
44 |
Correct |
10 ms |
492 KB |
Output is correct |
45 |
Correct |
8 ms |
492 KB |
Output is correct |
46 |
Correct |
8 ms |
492 KB |
Output is correct |
47 |
Correct |
9 ms |
628 KB |
Output is correct |
48 |
Correct |
9 ms |
492 KB |
Output is correct |
49 |
Correct |
9 ms |
492 KB |
Output is correct |
50 |
Correct |
7 ms |
492 KB |
Output is correct |
51 |
Correct |
7 ms |
492 KB |
Output is correct |
52 |
Correct |
6 ms |
492 KB |
Output is correct |
53 |
Correct |
7 ms |
492 KB |
Output is correct |
54 |
Correct |
5 ms |
492 KB |
Output is correct |
55 |
Correct |
4 ms |
492 KB |
Output is correct |
56 |
Correct |
4 ms |
492 KB |
Output is correct |
57 |
Correct |
4 ms |
512 KB |
Output is correct |
58 |
Correct |
4 ms |
492 KB |
Output is correct |
59 |
Correct |
5 ms |
492 KB |
Output is correct |
60 |
Correct |
7 ms |
492 KB |
Output is correct |
61 |
Correct |
5 ms |
492 KB |
Output is correct |
62 |
Correct |
4 ms |
492 KB |
Output is correct |
63 |
Correct |
5 ms |
492 KB |
Output is correct |
64 |
Correct |
5 ms |
492 KB |
Output is correct |
65 |
Correct |
8 ms |
492 KB |
Output is correct |
66 |
Correct |
7 ms |
492 KB |
Output is correct |
67 |
Correct |
8 ms |
492 KB |
Output is correct |
68 |
Correct |
7 ms |
492 KB |
Output is correct |
69 |
Correct |
6 ms |
492 KB |
Output is correct |
70 |
Correct |
6 ms |
464 KB |
Output is correct |
71 |
Correct |
7 ms |
492 KB |
Output is correct |
72 |
Correct |
4 ms |
492 KB |
Output is correct |
73 |
Correct |
5 ms |
492 KB |
Output is correct |
74 |
Correct |
2 ms |
492 KB |
Output is correct |
75 |
Execution timed out |
4065 ms |
15648 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |