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 <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(100010);
    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;
}

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

//define
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1)cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1)cout<<#x<<":"<<x<<endl;
#define ll long long

//typedef
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

bool possible(int num, vector<int> &a, vector<int> &b) {
	int cnt = 0;
	num++;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] <= num && num <= b[i]) {
			cnt++;
		}
	}
	if (cnt >= num)
		return true;
	else
		return false;

}

int main(){
	int n;
	cin >> n;
		vector<int> a(n), b(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		int lb = 0;
		int ub = n +1;
		while (ub - lb > 1) {
			int mid = (ub + lb) / 2;
			if (possible(mid,a,b)) {
				lb = mid;
			}
			else {
				ub = mid;
			}
			
		}

		cout << lb << endl;


	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int n;
int imos[111111];

int main(){
	cin.sync_with_stdio(false);
  cin>>n;
  rep(i,n){
    int a,b;
    cin>>a>>b;
    a--; b--;
    imos[a]++; imos[b+1]--;
  }
  rep(i,101010)imos[i+1]+=imos[i];
  int res=0;
  //rep(i,10)dbg(imos[i]);
  rep(i,111111){
    if(imos[i]>=i)maxch(res,i);
  }
  cout<<res<<endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<map>
#include<string>
#include<algorithm>
#include <vector>

using namespace std;

typedef struct term {
    int l, u;
}term;


int N;
vector < term > t;


bool search(int n){
    int count = 0;
    for(int i = 0; i < N; ++i){
        if(t[i].l - 1 <= n && t[i].u - 1 >= n){
            count++;
        }
        if(count == n) return true;
    }
    return false;
}



int main(){
    cin >> N;
    int ans = 0;
    t.resize(N);
    for(int i = 0; i < N; ++i){
        scanf("%d%d",&t[i].l,&t[i].u);
        //cin >> t[i].l >> t[i].u;
    }

    int min = 1, max = N;
    for(int i = 1; i < N + 1; ++i){
        if(search(i)) ans = i;
    }
    printf("%d",ans);
    //cout << ans << endl;
    
    return 0;       
}

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

#define rep(i,x,y) for(int (i)=(x);(i)<(y);(i)++)
#define INF 1000000000

vector<int> A;
vector<int> B;

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

  vector<int> F(N+1,0);
  rep(i,0,N){
    int a,b;
    cin>>a>>b;
    a--;
    F[a]++;
    if(b<N){
      F[b]--;
    }
  }
  rep(i,0,N){
    F[i+1]+=F[i];
  }

  int ans=0;
  rep(i,0,N+1){
    if(F[i]>i){
      ans=max(ans,i);
    }
  }

  cout<<ans<<endl;

  return 0;
}

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

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

    int a[100002] = {0}, b[100002] = {0};
    int temp;
    for(int i=0; i<n; i++){
        cin >> temp;
        a[temp-1]++;
        cin >> temp;
        b[temp]++;
    }

    int ans = 0;
    for(int i=100001; i>0; i--){
        ans = ans + b[i] - a[i];
        if(ans >= i){
            cout << i-1 << endl;
            break;
        }
    }
    cout << 0 << endl;
  
    return 0;
}