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 <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;char c;char str[1024];string s;ll l;
	int nextInt(){scanf("%d", &n);return n;}
	ll nextLong(){scanf("%lld", &l);return l;}
	char nextChar(){scanf("%c", &c);return c;}
	string next(){scanf("%s", str);return string(str);}
	string nextLine(){getline(cin, s);return s;}
};
Input in;

int c[100100];

int main(){
	int n = in.nextInt();
	vi a(n), b(n);
	rep(i, n) a[i] = in.nextInt(), b[i] = in.nextInt();

	vector<pii> p;
	rep(i, n) p.pb({a[i], b[i]});
	sort(all(p));

	int ans = 0;

	int idx1 = 0, idx2 = 0;
	for(int i = 0; i < n + 1; i++){
		//i + 1 nin de ikeru tomodachi no kazu
		int idx = upper_bound(all(p), mp(i + 1, INF)) - p.begin();
		//c[x] := x nin ijou ha dame
		for(int j = idx1; j < idx; j++){
			if(p[j].se < i) c[i]++;
			else c[p[j].se]++;
		}
		idx1 = idx;
		if(i > 0) c[i] += c[i-1];
		// i + 1 nin atumaranai
		if(i > idx) continue;
		//tomodachi ha i nin
		ans = max(ans, min(i, idx - c[i]));
	}

	cout<<ans<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int N;
	cin >> N;
	vector<int> sum(100003);
	for (int i = 0, a, b; i < N; i++) {
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	int res = -1;
	for (int i = 1; i <= 100002; i++) {
		sum[i] += sum[i - 1];
		if (sum[i] + 1 >= i) {
			res = i;
		}
	}
	cout << res - 1 << endl;
	return 0;
}

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

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int Digit[100002];
	for(int i = 0; i < 100002; ++i)
	{
		Digit[i] = 0;
	}
	int N;
	cin >> N;
	vector<P> Person(N);
	for(int i = 0; i < N; ++i)
	{
		cin >> Person[i].first >> Person[i].second;
	}
	for(int i = 0; i < N; ++i)
	{
		for(int j = Person[i].first; j <= Person[i].second; ++j)
		{
			++Digit[j];
		}
	}
	int Max = 0;
	for(int i = 2; i <= N; ++i)
	{
		if(Digit[i] + 1 >= i)
		{
			Max = i - 1;
		}
	}
	cout << Max << endl;
}

int main()
{
	solve();
	return(0);
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
    int n;  cin>>n;
    int OK[n][2];   for(int i=0;i<n;i++) cin>>OK[i][0]>>OK[i][1];

    int max=0;
    
    int result[n+2];  for(int i=0;i<n+2;i++)result[i]=0;

    for(int i=1;i<=n+1;i++){ // i は（私を含め）行く人数
        for(int j=0;j<n;j++){
            if(OK[j][0]<=i && i<=OK[j][1])result[i]++;
        }

        if(i<=result[i])result[i]=i-1;
        else if(i-1>result[i])result[i]=-1;

        if(result[i]>max)max=result[i];
    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

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

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

//    int max=0;
//    for(int i=0;i<n+2;i++){
//        if(result[i]>max)max=result[i];
//    }

    cout<<max<<endl;


    return 0;
}

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

using namespace std;

template <class T>
struct SegmentTree/*{{{*/
{
    vector<T> nodes, stocks;
    int size;
    SegmentTree(int size) : size(size) {
        nodes.resize(size*4, 0);
        stocks.resize(size*4, 0);
    }

    void maintain_consistency(int pos) {
        /*
        // ノードの値が子の合計と一致しない場合，不完全な更新が行なわれているため
        // 下のノードに伝搬させる．
        int children_sum = nodes[pos*2+1] + nodes[pos*2+2];
        if(children_sum != nodes[pos]) { 
            //cout << "maintain: " << pos << ' ' << nodes[pos] << ' ' << children_sum << endl;
            int diff = nodes[pos] - children_sum;
            nodes[pos*2+1] += diff/2;
            nodes[pos*2+2] += diff/2;
        }
        */
        if(stocks[pos] > 0) {
            stocks[pos*2+1] += stocks[pos] / 2;
            stocks[pos*2+2] += stocks[pos] / 2;
            nodes[pos] += stocks[pos];
            stocks[pos] = 0;
        }
    }

    // [left, right) に対するクエリ．
    // 現在のノードはpos で， [pl, pr) を表わしている．
    T get_inner(int left, int right, int pos, int pl, int pr) {
        //cout << left << ' ' << right << ' ' << pos << ' ' <<  pl << ' ' << pr << endl;
        if(pr <= left || right <= pl) return 0; // 交差しない
        if(left <= pl && pr <= right) return nodes[pos] + stocks[pos]; // 完全に含まれる

        maintain_consistency(pos);

        const int center = (pl+pr) / 2;
        T lv = get_inner(left, right, pos*2+1, pl, center);
        T rv = get_inner(left, right, pos*2+2, center, pr);
        return lv + rv;
    }

    T get(int left, int right) {
        return get_inner(left, right, 0, 0, size);
    }

    T add_inner(int left, int right, int pos, int pl, int pr) {
        //cout << left << ' ' << right << ' ' << pos << ' ' <<  pl << ' ' << pr << endl;
        if(pr <= left || right <= pl) return stocks[pos] + nodes[pos]; // 交差しない
        if(left <= pl && pr <= right) {
            //cout << "tail: " << pl << ' ' << pr << endl;
            stocks[pos] += pr-pl;
            return stocks[pos] + nodes[pos]; // 完全に含まれる
        }

        maintain_consistency(pos);

        const int center = (pl+pr)/2;
        T lv = add_inner(left, right, pos*2+1, pl, center);
        T rv = add_inner(left, right, pos*2+2, center, pr);
        return nodes[pos] = lv+rv;
    }

    T add(int left, int right) {
        return add_inner(left, right, 0, 0, size);
    }
};/*}}}*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int N;
    cin >> N;
    SegmentTree<int> segtree(100002);
    for(int i = 0; i < N; ++i) {
        int minval, maxval;
        cin >> minval >> maxval;
        segtree.add(minval-1, maxval);
    }
    //cout << "Added" << endl;
    //cout << segtree.nodes[0] << ' ' << segtree.nodes[1] << ' ' << segtree.nodes[2] << endl;
    int ans = 0;
    for(int n = N; n >= 0; --n) {
        //cout << segtree.nodes[0] << ' ' << segtree.nodes[1] << ' ' << segtree.nodes[2] << endl;
        const int val = segtree.get(n, n+1);
        //cout << val << endl;
        if(val >= n) {
            ans = n;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}