Problem:
Score : 1500 points

Problem Statement

There is an undirected connected graph with N vertices numbered 1 through N.
The lengths of all edges in this graph are 1.
It is known that for each i (1≦i≦N), the distance between vertex 1 and vertex i is A_i, and the distance between vertex 2 and vertex i is B_i.
Determine whether there exists such a graph.
If it exists, find the minimum possible number of edges in it.

Constraints

2≦N≦10^5

0≦A_i,B_i≦N-1

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

If there exists a graph satisfying the conditions, print the minimum possible number of edges in such a graph. Otherwise, print -1.

Sample Input 1

4
0 1
1 0
1 1
2 1

Sample Output 1

4

The figure below shows two possible graphs. The graph on the right has fewer edges.

Sample Input 2

3
0 1
1 0
2 2

Sample Output 2

-1

Such a graph does not exist.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

int N;
vector<int> A[100000];

void fail()
{
    printf("-1\n");
    exit(0);
}

int main()
{
    scanf("%d", &N);
    int maxb=0;
    int tmp=0;
    for(int i=0; i<N; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        if(i==0 && a!=0)
            fail();
        if(i==1 && b!=0)
            fail();
        if(i==0)
            tmp=b;
        if(i==1 && tmp!=a)
            fail();
        if(b==0 && i!=1)
            fail();
        A[a].push_back(b);
        maxb=max(maxb, b);
    }
    if(A[0].size()!=1)
        fail();
    for(int i=0; i<N; i++)
        sort(A[i].begin(), A[i].end());
    int ans=N-1;
    int maxi=0;
    for(int i=1; i<N; i++) if(!A[i].empty())
        maxi=i;
    for(int i=1; i<=maxi; i++)
    {
        if(A[i].empty())
            fail();
        for(auto& y: A[i])
        {
            int l=lower_bound(A[i-1].begin(), A[i-1].end(), y-1)-A[i-1].begin();
            int r=upper_bound(A[i-1].begin(), A[i-1].end(), y+1)-A[i-1].begin();
            if(l==r)
                fail();
            bool okl=y==0;
            bool okr=y==maxb;
            if(A[i-1][l]==y-1)
                okl=true;
            if(A[i-1][r-1]==y+1)
                okr=true;
            if((y!=0 && A[i-1][l]==y) && (y!=maxb && A[i-1][r-1]==y))
                ans++;
            if(i+1<=maxi)
            {
                l=lower_bound(A[i+1].begin(), A[i+1].end(), y-1)-A[i+1].begin();
                r=upper_bound(A[i+1].begin(), A[i+1].end(), y+1)-A[i+1].begin();
                if(l==r)
                    fail();
                if(A[i+1][l]==y-1)
                    okl=true;
                if(A[i+1][r-1]==y+1)
                    okr=true;
                if((y!=0 && A[i+1][l]==y) && (y!=maxb && A[i+1][r-1]==y))
                    ans++;
            }
            if(!okl || !okr)
            {
                l=lower_bound(A[i].begin(), A[i].end(), y-1)-A[i].begin();
                r=upper_bound(A[i].begin(), A[i].end(), y+1)-A[i].begin();
                if(l==r)
                    fail();
                if(!okl && A[i][l]==y-1)
                    okl=true, ans++;
                if(!okr && A[i][r-1]==y+1)
                    okr=true, ans++;
            }
            if(!okl || !okr)
                fail();
        }
    }
    printf("%d\n", ans);
    return 0;
}