#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define PB push_back
#define int long long
#define _sz(e) (int)e.size()
#define pii pair <int , int>
#define _all(e) e.begin() , e.end()
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse4,avx,avx2")
const ll maxn = 1e5 + 4 , N = 2e6 + 4 , mod = 1e9 + 7 , INF = 1e18 + 4 , lg = 43;
int n , k , sumL , sumR , sumT , pre[maxn] , suf[maxn];
priority_queue <int> L , R;
vector <pii> vec;
inline bool cmp(pii a , pii b) {
return a.F + a.S < b.F + b.S;
}
void add(int x) {
int med = (_sz(L) ? L.top() : INF);
if(x <= med) L.push(x) , sumL += x;
else R.push(-x) , sumR += x;
if(_sz(L) < _sz(R) - 1) {
int cur = R.top();
R.pop(); L.push(-cur);
sumL += -cur , sumR -= -cur;
}
else if (_sz(L) - 1 > _sz(R)) {
int cur = L.top();
L.pop(); R.push(-cur);
sumR += cur , sumL -= cur;
}
}
int32_t main() {
FAST;
cin >> k >> n;
for (int i = 0; i < n; ++i) {
char a , b; int s , t;
cin >> a >> s >> b >> t;
if(a == b) sumT += abs(s - t);
else vec.PB({s , t});
}
sort(_all(vec) , cmp);
for (int i = 0; i < _sz(vec); ++i) {
add(vec[i].F) , add(vec[i].S);
int med = L.top();
pre[i + 1] = (_sz(L) * med - sumL) + (sumR - _sz(R) * med);
}
while(_sz(L)) L.pop();
while(_sz(R)) R.pop();
sumL = sumR = 0;
for (int i = _sz(vec) - 1; i >= 0; --i) {
add(vec[i].F) , add(vec[i].S);
int med = L.top();
suf[_sz(vec) - i] = (_sz(L) * med - sumL) + (sumR - _sz(R) * med);
}
if(k == 1) {
cout << pre[_sz(vec)] + sumT + _sz(vec) << '\n';
}
else {
int ans = INF;
for (int i = 0; i <= _sz(vec); ++i) {
ans = min(ans , pre[i] + suf[_sz(vec) - i]);
}
cout << ans + sumT + _sz(vec) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
50 ms |
7068 KB |
Output is correct |
13 |
Correct |
96 ms |
6552 KB |
Output is correct |
14 |
Correct |
78 ms |
5816 KB |
Output is correct |
15 |
Correct |
54 ms |
4168 KB |
Output is correct |
16 |
Correct |
54 ms |
6100 KB |
Output is correct |
17 |
Correct |
78 ms |
6528 KB |
Output is correct |
18 |
Correct |
64 ms |
6328 KB |
Output is correct |
19 |
Correct |
95 ms |
6540 KB |
Output is correct |
20 |
Correct |
57 ms |
6580 KB |
Output is correct |
21 |
Correct |
86 ms |
6588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
46 ms |
6836 KB |
Output is correct |
26 |
Correct |
72 ms |
6708 KB |
Output is correct |
27 |
Correct |
102 ms |
6884 KB |
Output is correct |
28 |
Correct |
98 ms |
6804 KB |
Output is correct |
29 |
Correct |
98 ms |
6456 KB |
Output is correct |
30 |
Correct |
50 ms |
3716 KB |
Output is correct |
31 |
Correct |
50 ms |
7228 KB |
Output is correct |
32 |
Correct |
81 ms |
6896 KB |
Output is correct |
33 |
Correct |
60 ms |
7196 KB |
Output is correct |
34 |
Correct |
86 ms |
6856 KB |
Output is correct |
35 |
Correct |
58 ms |
6840 KB |
Output is correct |
36 |
Correct |
86 ms |
6888 KB |
Output is correct |
37 |
Correct |
50 ms |
6808 KB |
Output is correct |