#include <bits/stdc++.h>
using namespace std;
#define int long long
int K, N, ext, ans, c, suf[1<<17];
vector<array<int, 2>> a;
priority_queue<int> L, R;
void put(int v) {
if(empty(L) || v <= L.top())
L.push(+v), c -= v;
else
R.push(-v), c += v;
if(size(L) < size(R))
L.push(v = -R.top()), R.pop(), c -= v*2;
if(size(L) > size(R) + 1)
R.push(v = -L.top()), L.pop(), c -= v*2;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> K >> N;
for(int i = 0, s, t; i < N; ++i) {
char p, q;
cin >> p >> s >> q >> t;
if(p == q) ext += abs(t - s);
else
++ext, a.push_back({s, t});
}
N = size(a);
sort(begin(a), end(a) , [&](const auto &i, const auto &j) {
return i[0] + i[1] < j[0] + j[1];
});
for(int i = N; i--; ) {
put(a[i][0]), put(a[i][1]);
ans = suf[i] = c;
}
if(K > 1) {
L = R = priority_queue<int> (), c = 0;
for(int i = 0; i < N; ++i) {
put(a[i][0]), put(a[i][1]);
ans = min(ans, c + suf[i+1]);
}
}
cout << ans + ext;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
332 KB |
Output is correct |
12 |
Correct |
24 ms |
5700 KB |
Output is correct |
13 |
Correct |
49 ms |
5736 KB |
Output is correct |
14 |
Correct |
37 ms |
5304 KB |
Output is correct |
15 |
Correct |
27 ms |
3044 KB |
Output is correct |
16 |
Correct |
30 ms |
5648 KB |
Output is correct |
17 |
Correct |
35 ms |
5676 KB |
Output is correct |
18 |
Correct |
36 ms |
6088 KB |
Output is correct |
19 |
Correct |
40 ms |
5684 KB |
Output is correct |
20 |
Correct |
31 ms |
5692 KB |
Output is correct |
21 |
Correct |
39 ms |
5656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 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 |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 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 |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 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 |
28 ms |
5648 KB |
Output is correct |
26 |
Correct |
49 ms |
5612 KB |
Output is correct |
27 |
Correct |
58 ms |
5884 KB |
Output is correct |
28 |
Correct |
59 ms |
6024 KB |
Output is correct |
29 |
Correct |
59 ms |
5560 KB |
Output is correct |
30 |
Correct |
31 ms |
3164 KB |
Output is correct |
31 |
Correct |
35 ms |
5632 KB |
Output is correct |
32 |
Correct |
44 ms |
5564 KB |
Output is correct |
33 |
Correct |
46 ms |
6060 KB |
Output is correct |
34 |
Correct |
55 ms |
5640 KB |
Output is correct |
35 |
Correct |
36 ms |
5588 KB |
Output is correct |
36 |
Correct |
48 ms |
5600 KB |
Output is correct |
37 |
Correct |
23 ms |
5616 KB |
Output is correct |