#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);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
n = a.size();
vector<long long> pre1(n + 1), pre2(n + 1), v;
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];
set<long long> to;
for (auto x : a) v.push_back(x);
for (auto y : b) v.push_back(y);
to.insert(v[0]);
for (int i = 0; i < v.size() - 1; i++) {
to.insert(v[i + 1]);
to.insert(v[i] + v[i + 1] >> 1);
}
for (auto x : to) {
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';
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < v.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~
bridge.cpp:33:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | to.insert(v[i] + v[i + 1] >> 1);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |