#include <bits/stdc++.h>
#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : (c))
constexpr bool dbg = 1;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl
#define ll long long
#define pb push_back
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> arr(n, {-1, -1});
ll ans = 0;
rep(i, 0, 2 * n) {
int x, y;
cin >> x >> y;
if(x < 1) ans += 1 - x, x = 1;
if(x > n) ans += x - n, x = n;
if(y < 1) ans += 1 - y, y = 1;
if(y > 2) ans += y - 2, y = 2;
x--;
if(y == 1) arr[x].first++;
else arr[x].second++;
}
rep(i, 0, n) {
if(arr[i].first > 0 && arr[i].second < 0) {
auto move = min(arr[i].first, -arr[i].second);
arr[i].first -= move;
arr[i].second += move;
ans += move;
}
if(arr[i].first < 0 && arr[i].second > 0) {
auto move = min(-arr[i].first, arr[i].second);
arr[i].first += move;
arr[i].second -= move;
ans += move;
}
if(i == n - 1) continue;
arr[i + 1].first += arr[i].first, ans += abs(arr[i].first), arr[i].first = 0;
arr[i + 1].second += arr[i].second, ans += abs(arr[i].second), arr[i].second = 0;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |