#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 5;
int n, k;
ll lsum = 0, rsum = 0, pref[MAXN];
priority_queue <int> lpq;
priority_queue <int, vector <int>, greater <int>> rpq;
void insert(int x) {
int median = (lpq.size() ? lpq.top() : 1000000001);
if(x <= median) {
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if(rpq.size() + 1 < lpq.size()) {
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
lsum -= nxt, rsum += nxt;
} else if(lpq.size() < rpq.size()) {
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
rsum -= nxt, lsum += nxt;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll ss = 0, ans = 0;
cin >> k >> n;
vector <pair <int, int>> v = {{0, 0}};
for(int i = 0; i < n; i++) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b) ss += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), [&](pair <int, int> p, pair <int, int> q) {
return p.first + p.second < q.first + q.second;
});
n = (int)v.size(); ss += n - 1;
for(int i = 1; i < n; i++) {
insert(v[i].first);
insert(v[i].second);
pref[i] = rsum - lsum;
}
ans = pref[n - 1];
if(k == 2) {
while(lpq.size()) lpq.pop();
while(rpq.size()) rpq.pop();
lsum = rsum = 0;
for(int i = n - 1; i > 0; i--) {
insert(v[i].first);
insert(v[i].second);
ans = min(ans, rsum - lsum + pref[i - 1]);
}
}
cout << ss + ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
17 ms |
3832 KB |
Output is correct |
13 |
Correct |
35 ms |
5580 KB |
Output is correct |
14 |
Correct |
29 ms |
4560 KB |
Output is correct |
15 |
Correct |
20 ms |
3536 KB |
Output is correct |
16 |
Correct |
19 ms |
4824 KB |
Output is correct |
17 |
Correct |
26 ms |
5336 KB |
Output is correct |
18 |
Correct |
34 ms |
5328 KB |
Output is correct |
19 |
Correct |
31 ms |
5324 KB |
Output is correct |
20 |
Correct |
21 ms |
5332 KB |
Output is correct |
21 |
Correct |
30 ms |
5236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
464 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
520 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
30 ms |
3912 KB |
Output is correct |
26 |
Correct |
48 ms |
4560 KB |
Output is correct |
27 |
Correct |
60 ms |
4960 KB |
Output is correct |
28 |
Correct |
65 ms |
5780 KB |
Output is correct |
29 |
Correct |
61 ms |
5584 KB |
Output is correct |
30 |
Correct |
31 ms |
3100 KB |
Output is correct |
31 |
Correct |
26 ms |
4560 KB |
Output is correct |
32 |
Correct |
45 ms |
5320 KB |
Output is correct |
33 |
Correct |
57 ms |
5072 KB |
Output is correct |
34 |
Correct |
56 ms |
5336 KB |
Output is correct |
35 |
Correct |
31 ms |
5300 KB |
Output is correct |
36 |
Correct |
46 ms |
5068 KB |
Output is correct |
37 |
Correct |
19 ms |
4056 KB |
Output is correct |