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(1<<20);
    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 <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
using namespace std;
int main(int argc, char *argv[])
{
  for(;;) {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<pair<int,int>> abs;
    for(int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      abs.push_back(make_pair(a, b));
    }
    sort(abs.begin(), abs.end());
    int s = 0;
    int best = 0;
    std::priority_queue<int> heap;
    // heap = friends x s.t. abs[x].second >= k+1
    for(int k = 1; k <= n; k++) {
      while(s < n && abs[s].first <= k+1) {
        heap.push(-abs[s].second);
        s++;
      }
      while(heap.size() > 0 && -heap.top() <= k) heap.pop();
      if(heap.size() >= k) { best = k; }
    }
    cout << best << endl;
    break;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int people[100010];
int main() {
	int N;
	scanf("%d",&N);
	for(int i=0;i<N;i++) {
		int a,b;
		scanf("%d %d",&a,&b);
		people[a]++;
		people[b+1]--;
	}
	for(int i=1;i<=N+1;i++) {
		people[i]+=people[i-1];
	}
	int ans=0;
	for(int i=N;i>=0;i--) {
		if(people[i+1]>=i) {ans=i;break;}
	}
	printf("%d\n",ans);
}

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

using namespace std;

int c[2][100002], n, a, b;

main(){
  cin >> n;
  fill(c[0], c[2], 0);
  for(int i=0;i<n;i++){
    cin >> a >> b;
    c[0][a]++;
    c[1][b]++;
  }
  a = 0;
  b = 1;
  for(int i=1;i<n+3;i++){
    b += c[0][i];
    b -= c[1][i-1];
    if(b >= i) a = i - 1;
  }
  cout << a << endl;
}

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

const int MAX_N = 100010;
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 <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];
    static int a[100010], b[100010];
    scanf("%d", &n);
    rep (i, n)
	scanf("%d%d", a + i, b + i);
    rep (i, n)
    {
	++new_ok[min(b[i], n + 1)];
	--new_ok[min(a[i] - 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 <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    int s[100002] = {};
    for (int i = 0; i < n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        for (int j = a; j <= b; ++j)
            ++s[j];
    }

    int ans = 0;
    for (int i = 0; i <= n + 1; ++i)
        if (i == s[i])
            ans = i;
    printf("%d\n", max(ans - 1, 0));

    return 0;
}