#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << ": " << x << endl;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k, n;
cin >> k >> n;
ll ans = 0;
vector<int> coords;
vector<pair<int, int>> pairs;
for (int i = 0; i < n; ++i) {
char c1;
cin >> c1;
int x1;
cin >> x1;
char c2;
cin >> c2;
int x2;
cin >> x2;
if (c1 == c2) {
ans += abs(x1 - x2);
} else {
coords.push_back(x1);
coords.push_back(x2);
pairs.emplace_back(x1, x2);
++ans;
}
}
sort(coords.begin(), coords.end());
int m = coords.size();
if (k == 1) {
for (auto x : coords) {
ans += abs(x - coords[m / 2]);
}
} else {
const ll INF = 1e17;
ll mn = INF;
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
ll curr_ans = 0;
for (const auto& [x1, x2] : pairs) {
curr_ans += min(abs(x1 - coords[i]) + abs(x2 - coords[i]), abs(x1 - coords[j]) + abs(x2 - coords[j]));
}
mn = min(mn, curr_ans);
}
}
if (mn < INF) {
ans += mn;
}
}
cout << ans << '\n';
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# |
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 |
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 |
336 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 |
18 ms |
2112 KB |
Output is correct |
13 |
Correct |
37 ms |
2096 KB |
Output is correct |
14 |
Correct |
27 ms |
1992 KB |
Output is correct |
15 |
Correct |
22 ms |
1396 KB |
Output is correct |
16 |
Correct |
25 ms |
2112 KB |
Output is correct |
17 |
Correct |
26 ms |
2028 KB |
Output is correct |
18 |
Correct |
33 ms |
2096 KB |
Output is correct |
19 |
Correct |
47 ms |
2112 KB |
Output is correct |
20 |
Correct |
26 ms |
2068 KB |
Output is correct |
21 |
Correct |
33 ms |
2068 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 |
Correct |
5 ms |
320 KB |
Output is correct |
4 |
Correct |
5 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
212 KB |
Output is correct |
8 |
Correct |
6 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
316 KB |
Output is correct |
10 |
Correct |
6 ms |
212 KB |
Output is correct |
11 |
Correct |
6 ms |
320 KB |
Output is correct |
12 |
Correct |
6 ms |
324 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 |
Correct |
4 ms |
312 KB |
Output is correct |
4 |
Correct |
4 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
7 ms |
324 KB |
Output is correct |
8 |
Correct |
6 ms |
324 KB |
Output is correct |
9 |
Correct |
6 ms |
324 KB |
Output is correct |
10 |
Correct |
6 ms |
212 KB |
Output is correct |
11 |
Correct |
6 ms |
320 KB |
Output is correct |
12 |
Correct |
6 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2072 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
4 ms |
320 KB |
Output is correct |
4 |
Correct |
6 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
212 KB |
Output is correct |
8 |
Correct |
6 ms |
212 KB |
Output is correct |
9 |
Correct |
8 ms |
324 KB |
Output is correct |
10 |
Correct |
6 ms |
212 KB |
Output is correct |
11 |
Correct |
6 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2079 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |