#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
ll f(int x, vector<pii> &v) {
ll sm = 0;
for (auto i : v) sm += abs(i.ff - x) + 1 + abs(i.ss - x);
return sm;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int k, n;
cin >> k >> n;
ll sm = 0;
vector<pii> 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});
}
ll ans = LLONG_MAX;
for (auto i : v) ans = min({ans, f(i.ff, v), f(i.ss, v)});
cout << sm + ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |