#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long k, n, ans = 1e18, ss = 0, S = 0, S2 = 0;
cin >> k >> n;
vector<long long> a, b;
for (int i = 0; i < n; i++) {
char x, y;
int s, t;
cin >> x >> s >> y >> t;
if (x == y) {
ss += abs(s - t);
continue;
}
if (x == 'A') a.push_back(s), b.push_back(t);
else a.push_back(t), b.push_back(s);
}
if (a.size()) {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
n = a.size();
vector<long long> pre1(n + 1), pre2(n + 1);
for (int i = 0; i < n; i++) pre1[i + 1] = pre1[i] + a[i], S += a[i];
for (int i = 0; i < n; i++) pre2[i + 1] = pre2[i] + b[i], S2 += b[i];
for (auto x : a) {
long long k = upper_bound(a.begin(), a.end(), x) - a.begin();
long long k2 = upper_bound(b.begin(), b.end(), x) - b.begin();
ans = min(ans, 2 * (x * (k + k2 - n) - pre1[k] - pre2[k2]));
}
for (auto x : b) {
long long k = upper_bound(a.begin(), a.end(), x) - a.begin();
long long k2 = upper_bound(b.begin(), b.end(), x) - b.begin();
ans = min(ans, 2 * (x * (k + k2 - n) - pre1[k] - pre2[k2]));
}
cout << ans + S + S2 + ss + n << '\n';
} else {
cout << ss << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
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 |
# |
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 |
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 |
340 KB |
Output is correct |
12 |
Correct |
27 ms |
3648 KB |
Output is correct |
13 |
Correct |
60 ms |
3596 KB |
Output is correct |
14 |
Correct |
40 ms |
3352 KB |
Output is correct |
15 |
Correct |
36 ms |
2380 KB |
Output is correct |
16 |
Correct |
39 ms |
3740 KB |
Output is correct |
17 |
Correct |
44 ms |
3640 KB |
Output is correct |
18 |
Correct |
59 ms |
3620 KB |
Output is correct |
19 |
Correct |
61 ms |
3672 KB |
Output is correct |
20 |
Correct |
37 ms |
3648 KB |
Output is correct |
21 |
Correct |
52 ms |
3632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |