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 <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 1<<17;        // 131072

struct SegTree {
  int n;
  int dat[MAX_N * 2 - 1];
  SegTree(int _n) {
    for (n=1; n<_n; n*=2);
    fill(dat, dat+2*n-1, 0);
  }
  void up(int a, int b) { return update(a, b, 0, 0, n); } // [a,b)ðPÂÇÁ
  void update(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) dat[k]++;
    else {
      update(a, b, k*2+1, l, (l+r)/2);
      update(a, b, k*2+2, (l+r)/2, r);
    }
  }
  int q(int a) { return query(a, 0, 0, n); } // aðÜÞæÔÌ
  int query(int a, int k, int l, int r) {
    if (r <= a || a < l) return 0;
    if (r-l == 1) return dat[k];
    return dat[k] + query(a, k*2+1, l, (l+r)/2) + query(a, k*2+2, (l+r)/2, r);
  }
};

int main() {
  int n;
  while(cin >> n) {
    SegTree sg(100001);
    REP(i,n) {
      int a, b;
      cin >> a >> b;
      //cout << a << " " << b << endl;
      sg.up(a,b+1);
      //cout << "ok" << endl;
    }
    //cout << "hoge" << endl;
    for (int i=n; i>=0; --i) {
      //cout << i << endl;
      if (sg.q(i+1) >= i) {
        cout << i << endl;
        break;
      }
    }
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100002;

int main(){
  int n,ans[MAX],a,b;
  int MX = -1<<27;
  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    MX = max(MX,b);
    for(int i = a ; i <= b ; i++)
      ++ans[i];
  }
  int fri = 0;
  for(int i = 2 ; i <= MX ; i++){
    if(ans[i]+1 > i) fri = i-1; 
  }
  cout << fri << endl;

  return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<pair<int,int> > P(N);
    for (auto &i : P) cin >> i.first >> i.second;

    int l = 0, r = 100002;
    while (r - l > 1) {
        int m = (r + l) / 2;
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (P[i].first > m + 1 || P[i].second < m + 1) {
                count++;
            }
        }
        if (m <= N - count) {
            l = m;
        } else {
            r = m;
        }
    }

    int count = 0;
    for (int i = 0; i < N; i++) {
        if (P[i].first > r + 1 || P[i].second < r + 1) {
            count++;
        }
    }

    if (r <= N - count) cout << r << endl;
    else cout << l << endl;

    return 0;
}

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

int imos[111111];
int main(){
    int n;
    while(cin >> n){
        memset(imos,0,sizeof(imos));
        for(int i=0;i<n;i++){
            int a,b;
            cin>>a>>b;
            imos[a-1]++;
            imos[b]--;
        }
        for(int i=1;i<111111;i++){
            imos[i]+=imos[i-1];
        }
        int ans=0;
        for(int i=0;i<111111;i++){
            if(imos[i]>=i)ans=max(ans,i);
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int bit[100010];
int N = 0;

void init(int n) {
	N = n;
	rep(i, N)	bit[i] = 0;
}

void add(int k, int a) {
	while (k <= N) {
		bit[k] += a;
		k += k&-k;
	}
}

int sum(int k) {
	int ret = 0;
	while (k > 0) {
		ret += bit[k];
		k -= k&-k;
	}
	return ret;
}

int main() {
	int n;	cin >> n;
	vector<pii> a(n, mp(0, 0));
	rep(i, n)	cin >> a[i].fst >> a[i].scn;
	sort(a.begin(), a.end());
	//????°??????°???n????????§??????????????¶??????????????§?????????
	while (a.size() > 0 && n + 1 < a[n - 1].fst)	a.erase(a.begin() + (--n));
	int big = 0;
	rep(i, n)	big = max(big, a[i].scn);
	big++;
	init(big);
	//????°??????°????°??????????????????£??????????????§????????§?????°???BIT??§??????
	int ret = 1;
	rep(i, n) {
		ret = max(ret, i + 2 - sum(a[i].fst));
		add(a[i].scn,1);
	}
	cout << ret - 1 << endl;
	return 0;
}

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

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

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

int main(void){
	int n;
	std::cin >> n;
	int endmax = 0;

	int* pn = new int[100000];
	memset(pn, 0, 100000 * sizeof(int));

	for(int i=0; i<n; i++){
		int beg, end;
		std::cin >> beg >> end;
		for(int i = beg; i <= end; i++) pn[i]++;
		if(end > endmax) endmax = end;
	}

	int mps = 1;
	for(int i=1; i<=endmax; i++){\
		if(pn[i]+1 >= i)mps = i;
	}

	std::cout << (mps-1) << '\n';
	return 0;
}

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

using namespace std;

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

	vector< pair<int,int> > data(N);
	int nmin = 1000001;
	int nmax = 0;
	for (int i = 0; i < N; ++i) {
		cin >> data[i].first >> data[i].second;
		nmin = min(nmin,data[i].first);
		nmax = max(nmax,data[i].second);
	}
	nmax = min(N+1,nmax);

	sort(data.begin(),data.end());

	bool find = false;
	int res = 0;
	for (int i = nmax; !find && i >= nmin; --i) {
		int count = 0;
		for (int j = 0; j < N; ++j) {
			if (data[j].first <= i && data[j].second >= i) {
				++count;
			}
		}
		if (count >= i-1) {
			find = true;
			res = i-1;
		}
	}
	cout << res << endl;
	return 0;
}