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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
           
#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
    int n;
    static int new_ok[100010];
    scanf("%d", &n);
    rep (i, n)
    {
	int a, b;
	scanf("%d%d", &a, &b);
	++new_ok[min(b, n + 1)];
	--new_ok[min(a - 1, n + 1)];
    }
    ++new_ok[n + 1];

    int res = -3, ok = 0;
    for (int i = n + 1; i > 0; --i)
    {
	if (new_ok[i])
	    ok += new_ok[i];
	if (ok >= i)
	{
	    res = i - 1;
	    break;
	}
    }
    printf("%d\n", res);
}

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

const int MAX_N = 100001;
int s[MAX_N] = {0};

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

C++: #include <iostream>

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


using namespace std;

int main(){
	int n, a, b, fr[100005], ans=0;
	cin >> n;
	REP(i,100005){ fr[i] = 0;}
	fr[0] = 1;
	REP(i,n){
		cin >> a >> b;
		fr[a]++;
		fr[b+1]--;
	}
	REP(i,100004){
		fr[i+1] += fr[i];
	}
	for (int i=0; i<=n; i++){
		if (fr[i]>ans && fr[i]>=i){
			ans = min(i, fr[i]);
		}
	}
	
	
	//REP(i,10) cerr << fr[i] << " ";
	//cerr << endl;
	
	cout << ans-1 << endl;
	
}

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

const int MAX_N = int(1e5);
int N;
int dups[MAX_N + 3];

void init() {
	scanf("%d", &N);
	for (int _ = 0; _ < N; ++_) {
		int from, to;
		scanf("%d%d", &from, &to);
		++dups[from-1];
		--dups[to];
	}
}

int solve() {
	for (int i = 0; i < MAX_N + 2; ++i) {
		dups[i+1] += dups[i];
	}
	for (int i = N; i >= 0; --i) {
		if (dups[i] >= i) {
			return i;
		}
	}
	return 0;
}

int main() {
	init();
	printf("%d\n", solve());
	return 0;
}

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

int s[100001] = {0};

int main(){
	int n;
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		int a, b;
		cin >> a >> b;
		s[a]++, s[b+1]--;
	}
	
	for(int i=1 ; i < 100001 ; i++ ){
		s[i] += s[i-1];
	}
	int ans = 1;
	for(int i=1 ; i < 10001 ; i++ ){
		if( i-1 <= s[i] ) ans = i;
	}
	ans--;
	cout << ans << endl;
}

C++: #include<bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::pair<int,int> PII;

typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;
typedef std::vector<PII> VPII;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

int N;
VI cnt(100005, 0);
int main() {
    std::cin >> N;

    int a, b;
    REP(i, N) {
        std::cin >> a >> b;
        FOR(i, a, b+1) {
            ++cnt[i];
        }
    }

    int ans = 0;
    FOR(i, 2, 100002) {
        if(i-1 <= cnt[i]) {
            ans = i-1;
        }
    }
    std::cout << ans << std::endl;
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;



signed main() {
	cin1(n);

	vector<int> num(100002,0);
	int max = 0;
	for (int i = 0; i < n; i++) {
		int a,b; cin2(a,b);
		num[a]++;
		num[b+1]--;
		if (max < b) max = b;
	}
	
	int sum = 0;
	int ans = 0;

	for (int i = 0; i <= max; i++) {
		sum += num[i];	
		if (sum + 1 >= i) ans = i - 1;
	}
	
	cout(ans);

	return 0;
}

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

int main(){
	int a[100005] = {};
	int n,x,y,res = 0;
	a[1] = 1;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> x >> y;
		a[x]++;
		a[y]--;
	}
	for(int i = 0;i < 100005;i++){
		a[i+1] += a[i];
	}
	for(int i = 0;i < 100005;i++){
		if(a[i] >= i){
			res = max(res,a[i]);
		}
	}
	cout << res - 1 <<endl;

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,table[100002]={0};
  cin >>n;
  for(int i=0,a,b; i<n; i++){
    cin >>a>>b;
    for(a; a<=b; a++) table[a]++;
  }
  for(int i=n+1; i>0; i--){
    if(table[i] >= i-1){
      cout <<i-1<<endl;
      return 0;
    }
  }
  cout <<"0"<<endl;
  return 0;
}