#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int K, n;
cin >> K >> n;
ll sm = 0;
vector<vector<int>> v;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) sm += abs(t - s);
else v.push_back({s + t, s, t});
}
if (v.empty()) return cout << sm, 0;
sm += (int)v.size();
sort(v.begin(), v.end());
vector<int> u;
for (auto i : v) u.insert(u.end(), {i[1], i[2]});
if (K == 1) {
sort(u.begin(), u.end());
for (int j = 0; j < (int)u.size(); j++) sm += (j < (int)u.size() >> 1 ? -1 : 1) * u[j];
return cout << sm, 0;
}
ll mn = LLONG_MAX;
for (int i = 0; i < (int)u.size(); i++) {
for (int j = 0; j < (int)u.size(); j++) {
ll sm = 0;
for (auto k : v) sm += min(abs(k[1] - u[i]) + abs(k[2] - u[i]), abs(k[1] - u[j]) + abs(k[2] - u[j]));
mn = min(mn, sm);
}
}
cout << sm + mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
532 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
532 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
28 ms |
10328 KB |
Output is correct |
13 |
Correct |
48 ms |
11776 KB |
Output is correct |
14 |
Correct |
38 ms |
9996 KB |
Output is correct |
15 |
Correct |
29 ms |
6864 KB |
Output is correct |
16 |
Correct |
30 ms |
11208 KB |
Output is correct |
17 |
Correct |
30 ms |
11780 KB |
Output is correct |
18 |
Correct |
34 ms |
11272 KB |
Output is correct |
19 |
Correct |
38 ms |
11776 KB |
Output is correct |
20 |
Correct |
30 ms |
11264 KB |
Output is correct |
21 |
Correct |
37 ms |
11516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
42 ms |
348 KB |
Output is correct |
4 |
Correct |
44 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
61 ms |
444 KB |
Output is correct |
8 |
Correct |
62 ms |
456 KB |
Output is correct |
9 |
Correct |
61 ms |
344 KB |
Output is correct |
10 |
Correct |
62 ms |
348 KB |
Output is correct |
11 |
Correct |
62 ms |
344 KB |
Output is correct |
12 |
Correct |
67 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
42 ms |
456 KB |
Output is correct |
4 |
Correct |
44 ms |
348 KB |
Output is correct |
5 |
Correct |
10 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
61 ms |
348 KB |
Output is correct |
8 |
Correct |
62 ms |
452 KB |
Output is correct |
9 |
Correct |
61 ms |
348 KB |
Output is correct |
10 |
Correct |
64 ms |
348 KB |
Output is correct |
11 |
Correct |
62 ms |
348 KB |
Output is correct |
12 |
Correct |
65 ms |
344 KB |
Output is correct |
13 |
Execution timed out |
2100 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
42 ms |
344 KB |
Output is correct |
4 |
Correct |
48 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
64 ms |
348 KB |
Output is correct |
8 |
Correct |
61 ms |
348 KB |
Output is correct |
9 |
Correct |
62 ms |
344 KB |
Output is correct |
10 |
Correct |
68 ms |
348 KB |
Output is correct |
11 |
Correct |
62 ms |
444 KB |
Output is correct |
12 |
Correct |
63 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |