This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 1e5 + 5;
int a[Nmax][3], s[3], wait[3], n;
ll ans = 0;
void read()
{
    int x, y, i;
    cin >> n;
    for(i=1; i<=2*n; ++i)
    {
        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;
        ++a[x][y];
    }
}
void solve()
{
    int i, j, k1, k2;
    k1 = 1; k2 = 0;
    for(i=1; i<=n; ++i)
        for(j=0; j<a[i][1] + a[i][2]; ++j)
        {
            if(k1 < i) ans += i - k1;
                else if(k1 > i) ans += k1 - i;
            if(k2) ++k1;
            k2 ^= 1;
        }
    for(i=1; i<=n; ++i)
    {
        ++wait[1];
        ++wait[2];
        s[1] += a[i][1];
        s[2] += a[i][2];
        while(wait[1] && s[1]) --wait[1], --s[1];
        while(wait[2] && s[2]) --wait[2], --s[2];
        while(wait[1] && s[2]) --wait[1], --s[2], ++ans;
        while(wait[2] && s[1]) --wait[2], --s[1], ++ans;
    }
    cout << ans << '\n';
}
int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false);
    read();
    solve();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |