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<bits/stdc++.h>

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

typedef std::vector<int>  VI;

int N;
VI imos(100002, 0);
int main() {
    std::cin >> N;

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

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
 
#define rep(i,n) for (int i = 0; i < (n); ++i)
  
int n, a, b, friends[100005], ans;
   
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::cin >> n;
  rep(i,n) {
    std::cin >> a >> b;
    for (int i = a; i <= b; ++i) friends[i]++;
  }
  rep(i,n+1) {
    ans = std::max(ans, friends[i]);
  }

  std::cout << ans-1 << std::endl;

  return 0;
}

C++: #include <cstdio>

using namespace std;

int main(void)
{
	int N;
	static int sum[100001];
	
	scanf("%d", &N);
	
	for (int i = 0; i < N; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		sum[--a]++;
		sum[b]--;
	}
	
	int ans = 0;
	for (int i = 1; i < 100001; i++){
		sum[i] += sum[i - 1];
		if (sum[i] >= i) ans = i;
	}
	
	printf("%d\n", ans);
	
	return (0);
}

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

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

#define int long long

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

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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<int> imosu(100010);
    imosu[0] += 1;
    imosu[imosu.size() - 1] -= 1;
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        imosu[a] += 1;
        imosu[b + 1] -= 1;
    }
    for (int i = 1; i < imosu.size(); i++) {
        imosu[i] = imosu[i] + imosu[i - 1];
    }
    int ans = 0;
    for (int i = 0; i < imosu.size(); i++) {
        if (i <= imosu[i]) {
            ans = max(i - 1, ans);
        }
    }
    cout << ans << endl;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

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

using namespace std;
int main(void)
{
  for(;;){
    int n;
    cin >> n;
    if(!n) return 0;
    vector<int> f(131072,0);
    REP(i,n){
      int l,r;
      cin >> l >> r;
      ++f[l];
      --f[r+1];
    }
    ++f[0];
    REP(i,131000) f[i+1]+=f[i];
    int answer=0;
    REP(i,131000) if(f[i]>=i) answer=i-1;
    cout << answer << endl;
  }
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int i;
  int n;
  cin>>n;
  int a[100000],b[100000];
  for(i=0;i<n;++i)
    cin>>a[i]>>b[i];
  int mn=1,mx=n+2;
  while(mn+1!=mx){
    cout<<mn<<" "<<mx<<endl;
    int ct=1;
    for(i=0;i<n;++i){
      if(a[i]<=(mn+mx)/2&&(mn+mx)/2<=b[i])
	++ct;
    }
    if(ct<(mn+mx)/2)
      mx=(mn+mx)/2;
    else
      mn=(mn+mx)/2;
  }
  cout<<mn-1<<endl;
  return 0;
}