#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k, n; cin >> k >> n;
ll ans = 0;
vector<array<ll, 2>> a;
for (int i = 0; i < n; i++) {
char pp, qq;
ll x, y;
cin >> pp >> x >> qq >> y;
if (x > y) swap(x, y);
if (pp == qq) ans += y - x;
else a.push_back({x, y}), ans++;
}
sort(a.begin(), a.end(), [&] (array<ll, 2> i, array<ll, 2> j) {
return i[0] + i[1] < j[0] + j[1];
});
n = a.size();
if (n == 0) {
cout << ans << en;
return 0;
}
ll d = 0;
priority_queue<ll> p, q;
vector<ll> pref(n), suff(n);
for (int i = 0; i < n; i++) {
p.push(a[i][0]); d -= a[i][0];
q.push(-a[i][1]); d += a[i][1];
if (p.top() > -q.top()) {
d += 2 * (p.top() + q.top());
ll xx = p.top(); ll yy = q.top();
p.pop(); q.pop();
p.push(-yy); q.push(-xx);
}
pref[i] = d;
}
d = 0;
while (!p.empty()) p.pop();
while (!q.empty()) q.pop();
for (int i = n - 1; i >= 0; i--) {
p.push(a[i][0]); d -= a[i][0];
q.push(-a[i][1]); d += a[i][1];
if (p.top() > -q.top()) {
d += 2 * (p.top() + q.top());
ll xx = p.top(); ll yy = q.top();
p.pop(); q.pop();
p.push(-yy); q.push(-xx);
}
suff[i] = d;
}
if (k == 1) {
cout << ans + suff[0] << en;
return 0;
}
ll ad = suff[0];
for (int i = 0; i < n - 1; i++) smin(ad, pref[i] + suff[i + 1]);
cout << ans + ad << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 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 |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
340 KB |
Output is correct |
12 |
Correct |
33 ms |
6408 KB |
Output is correct |
13 |
Correct |
81 ms |
6392 KB |
Output is correct |
14 |
Correct |
72 ms |
5852 KB |
Output is correct |
15 |
Correct |
50 ms |
3536 KB |
Output is correct |
16 |
Correct |
37 ms |
6300 KB |
Output is correct |
17 |
Correct |
73 ms |
6336 KB |
Output is correct |
18 |
Correct |
48 ms |
6316 KB |
Output is correct |
19 |
Correct |
96 ms |
6340 KB |
Output is correct |
20 |
Correct |
54 ms |
6344 KB |
Output is correct |
21 |
Correct |
79 ms |
6276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
34 ms |
7116 KB |
Output is correct |
26 |
Correct |
59 ms |
7340 KB |
Output is correct |
27 |
Correct |
79 ms |
8060 KB |
Output is correct |
28 |
Correct |
85 ms |
8716 KB |
Output is correct |
29 |
Correct |
85 ms |
8652 KB |
Output is correct |
30 |
Correct |
45 ms |
4424 KB |
Output is correct |
31 |
Correct |
42 ms |
8040 KB |
Output is correct |
32 |
Correct |
88 ms |
8664 KB |
Output is correct |
33 |
Correct |
45 ms |
8240 KB |
Output is correct |
34 |
Correct |
83 ms |
8768 KB |
Output is correct |
35 |
Correct |
51 ms |
8264 KB |
Output is correct |
36 |
Correct |
84 ms |
8448 KB |
Output is correct |
37 |
Correct |
30 ms |
7108 KB |
Output is correct |