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>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int sum[100100];

int main() {
	cin >> n;
	fill(sum, sum + 100100, 0);
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	int ans = 0;
	REP(i, n) {
		sum[i + 1] += sum[i];
	}
	FOR(i, 1, n + 1) {
		//cout << sum[i];
		if (i <= sum[i]) {
			if (sum[i] >= i)ans = max(ans, i - 1);
			else ans = max(ans, i);
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    int sum[100010]={};
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        sum[a]++;
        sum[b+1]--;
    }
    for(int i=0;i<100003;i++){
        sum[i+1]+=sum[i];
    }
    int ans=0;
    for(int i=2;i<=n;i++){
        if(i-1<=sum[i])ans=i-1;
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio> 
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)

class Solve {
public:
	Solve() {};
	~Solve() {};

	void solver() {
		int ans = 0;
		int n;
		std::cin >> n;
		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));
		rep(i, n) {
			std::cin >> friends[i].first >> friends[i].second;
			//Exclude myself
			friends[i].first--; friends[i].second--;
		}
		//the number of people to take i.
		rangeRep(i, 1, n+1) {
			int num = 0;
			//check if I can take friend.
			rep(j, n) {
				if (friends[j].first <= i && i <= friends[j].second) {
					num++;
				}
			}
			//There are enough people who said OK
			if (num >= i && ans < num) {
				ans = i;
			}
		}
		std::cout << ans << std::endl;
	}
private:

};

int main() {
	Solve solve;
	solve.solver();
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int main() {
  int N; cin >>N;
  vector<int> v(100010, 0);
  REP(i, N){
    int a, b; cin >>a >>b;
    v[a]++; v[b + 1]--;
  }
  FOR(i, 1, v.size()) v[i] += v[i - 1];
  int ans = 1;
  REP(i, v.size()) if(i <= v[i] + 1) ans = i - 1;
  cout <<ans <<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
using namespace std;
int main(){
    int n;
    int cnt[100100];
    memset(cnt,0,sizeof(cnt));
    cin>>n;
    REP(i,n){
        int a,b;
        cin>>a>>b;
        cnt[a]++;cnt[b+1]--;
    }
    int ans=0;
    REP(i,100000)cnt[i+1]+=cnt[i];
    REP(i,n+2){
        cout<<i<<endl;
        if(cnt[i]+1>=i){
            ans=i-1;
        }
    }
    cout<<ans<<endl;
}

C++: #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
	int n;
	cin>>n;
	vector<ll> h(100002, 0);
	REP(i, n) {
		ll a, b;
		cin>>a>>b;
		h[a] += 1;
		if(b + 1 < 100002) h[b + 1] -= 1;
	}
	FOR(i, 1, 100002) {
		h[i] += h[i - 1];
	}
	ll ans = 0;
	REP(i, 100002) {
		if(h[i] >= i - 1) ans = max(ans, i - 1);
	}
	cout<<ans<<endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=1; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=1; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, i);
  }
  cout << mx << endl;
   
  return 0;
}

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

#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;
using ll = long long;

int main(){
    int N;
    cin >> N;

    int arr[N + 1];
    memset(arr, 0, sizeof(arr));

    for (int i = 0; i < N; ++i) {
        int a, b;
        cin >> a >> b;

        // 2 4
        // 1−３ 人呼んでオッケー
        if (a - 1 <= N)
            arr[a - 1]++;
        if (b <= N)
            arr[b]--;
    }

    int maxn = arr[0];
    int maxi = 0;
    for (int i = 1; i <= N; ++i) {
        arr[i] += arr[i - 1];
        if (arr[i] > maxn && arr[i] >= i) {
            maxn = arr[i];
            maxi = i;
        }
    }

    cout << maxi << endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)

int main(){
    int n;
    cin>>n;
    int imos[1000001];
    rep(i,1000001)imos[i]=0;
    rep(i,n){
        int a,b;
        cin>>a>>b;
        imos[a]++;
        imos[b+1]--;
    }
    
    rep(i,1000000) imos[i+1] += imos[i];

    int ans = 0;
    rep(i,1000001)
        if(imos[i]+1>=i)
            ans = max<ll>(ans,i-1);
    
    cout<<ans<<endl;
}