#include <bits/stdc++.h>
#define int long long
using namespace std;
#define PQ priority_queue
#define PQG(x) PQ<x,vector<x>,greater<x>>
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second < b.first + b.second;
}
PQ<int> l;
PQG(int) r;
int ls, rs, arr[100100];
void ins(int x) {
int m = (l.size() ? l.top() : (int)(1e9+7));
if (x <= m) {
l.push(x);
ls += x;
} else {
r.push(x);
rs += x;
}
if (r.size() + 1 < l.size()) {
int x = l.top();
l.pop();
r.push(x);
ls -= x;
rs += x;
} else if (l.size() < r.size()) {
int x = r.top();
r.pop();
l.push(x);
rs -= x;
ls += x;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n;
int ans = 0;
vector<pair<int, int>> v = {{0, 0}};
cin >> k >> n;
for(int i = 0; i < n; i++){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) ans += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), cmp);
ans += n = v.size()-1;
for(int i = 1; i <= n; i++) {
ins(v[i].first);
ins(v[i].second);
arr[i] = rs - ls;
}
int ans2 = arr[n];
if (k == 2) {
l = PQ<int>();
r = PQG(int)();
ls = rs = 0;
for (int i = n+1; --i;) {
ins(v[i].first);
ins(v[i].second);
ans2 = min(ans2, rs - ls + arr[i - 1]);
}
}
cout << ans + ans2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
26 ms |
6412 KB |
Output is correct |
13 |
Correct |
52 ms |
7700 KB |
Output is correct |
14 |
Correct |
42 ms |
6980 KB |
Output is correct |
15 |
Correct |
36 ms |
4936 KB |
Output is correct |
16 |
Correct |
35 ms |
7020 KB |
Output is correct |
17 |
Correct |
41 ms |
7876 KB |
Output is correct |
18 |
Correct |
43 ms |
7816 KB |
Output is correct |
19 |
Correct |
46 ms |
7872 KB |
Output is correct |
20 |
Correct |
33 ms |
7476 KB |
Output is correct |
21 |
Correct |
46 ms |
7644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
33 ms |
6348 KB |
Output is correct |
26 |
Correct |
52 ms |
7232 KB |
Output is correct |
27 |
Correct |
64 ms |
7344 KB |
Output is correct |
28 |
Correct |
68 ms |
8352 KB |
Output is correct |
29 |
Correct |
69 ms |
7904 KB |
Output is correct |
30 |
Correct |
38 ms |
4172 KB |
Output is correct |
31 |
Correct |
39 ms |
6904 KB |
Output is correct |
32 |
Correct |
56 ms |
7868 KB |
Output is correct |
33 |
Correct |
54 ms |
7696 KB |
Output is correct |
34 |
Correct |
57 ms |
7860 KB |
Output is correct |
35 |
Correct |
41 ms |
7480 KB |
Output is correct |
36 |
Correct |
54 ms |
7672 KB |
Output is correct |
37 |
Correct |
29 ms |
6288 KB |
Output is correct |