Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #include <iostream>
using namespace std;

int main(){
  int n;

  while(cin>>n){
    int dec[100002];
    int inc[100002];

    for(int i=0;i<100002;i++){
      dec[i] = 0;
      inc[i] = 0;
    }

    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      inc[a]++;
      dec[b]++;
    }

    int now = 0;
    int ans = 0;

    for(int i=0;i<100002;i++){
      now += inc[i];
      if(now + 1 >= i) ans = max(ans, i);
      now -= dec[i];
    }

    cout<<ans-1<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<(S)<<endl
#define ck(a,b) (0<=(a)&&(a)<(b))

using namespace std;

int main(){
	int N,ans=1;
	cin>>N;
	int A[100002]={0};
	REP(i,0,N){
		int a,b;
		cin>>a>>b;
		A[a]++,A[b+1]--;
	}
	REP(i,3,N+1){
		A[i]+=A[i-1];
	}
	for(int i=N;N>=2;N--){
		if(A[i]>=i){
			ans=i;
			break;
		}
	}
	p(ans-1);
	return 0;
}

C++: #include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <locale>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main(void) {
    int n;
    cin >> n;

    vector<vector<int>> a(n, vector<int>(2));
    for (int i = 0; i < n; i++) {
        cin >> a[i][0];
        cin >> a[i][1];
    }
    int ans  = 0;
    int keep = 0;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < n; j++) {
    //         if (a[j][0] < i + 1 && a[j][1] > i + 1) keep++;
    //         if (keep > i + 1) break;
    //     }
    //     ans  = max(ans, keep);
    //     keep = 0;
    // }

    vector<int> imosu(100002, 0);
    // fill(imosu.begin(), imosu.end(), 0);

    for (int i = 0; i < n; i++) {
        // if (a[i][0] - 2 >= 0)
        //     imosu[a[i][0] - 1]++;
        // else
        imosu[a[i][0]]++;
        // if (a[i][1] <= n)
        imosu[a[i][1] + 1]--;
    }

    for (int i = 1; i <= n + 1; i++) {
        imosu[i] = imosu[i - 1] + imosu[i];
    }

    // for (int test : imosu) cout << test << " ";

    for (int i = 0; i <= n + 1; i++) {
        if (i <= imosu[i] + 1) {
            ans = max(ans, i);
        }
    }
    cout << ans - 1 << endl;
}

C++: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n;

main(){
	while(cin >> n){
		vector<int> im(100002);
		REP(i, n){
			int x, y;
			cin >> x >> y;
			im[x] ++;
			im[y++] --;
		}
		int ans = 0;
		REP(i, n+1){
			im[i+1] += im[i];
			if(im[i+1] >= i) ans = i;
		}
//		cout << im << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()


int main() {
    int n, imos[MAX_N]{}, ans=0;
    cin >> n;
    imos[0]++;
    REP(i,n){
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    REP(i,n+2){
        imos[i+1] += imos[i];
    }
    REP(i,n+2){
        if(imos[i]>=i) ans = i-1;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
        int N;
        cin >> N;
        vector<int> diff(100003);
        for(int i = 0; i < N; ++i){
                int a, b;
                cin >> a >> b;
                ++diff[a];
                --diff[b + 1];
        }
        int count = 1, answer = 1;
        for(int i = 2; i <= N + 1; ++i){
                count += diff[i];
                if(i <= count){
                        answer = max(answer, i);
                }
        }
        cout << answer - 1 << endl;
        return 0;
}

C++: #include<iostream>
using namespace std;
#define IMOSU_MAX 100010

int N;
int imosu[IMOSU_MAX];
int a,b;
int ans;

int main() {
	cin>>N;
	for (int i = 0; i < N; i++) {
		cin>>a>>b;
		imosu[a]++;
		imosu[b+1]--;
	}
	imosu[0]++;
	for (int i = 1; i < IMOSU_MAX; i++) {
		imosu[i]+=imosu[i-1];
		if(i>=ans && imosu[i]>=i)ans=i;
	}
	//for(int i=0;i<10;i++)cout<<" "<<imosu[i];
	cout<<ans-1<<endl;
	//cin>>N;
	return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    Int(n);
    vi ans(100003, 0);
    rep(i, n) {
        Int(a);
        Int(b);
        ans[a]++;
        ans[b+1]--;
    }
    int ret = 0;
    Rep(i, n+1) {
        ans[i] += ans[i-1];
        ret = max(ret, ans[i]);
    }
    cout << ret -1 << endl;

    return 0;
}