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 <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

long long mod_pow(long long x, long long n) {
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

void fast_input() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

int imos[100005];

int main(void) {
    int N; cin >> N;
    imos[0]++;
    imos[100002]--;

    for (int i = 0; i < N; i++) {
        int a, b; cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    for (int i = 1; i <= 100001; i++) {
        imos[i] += imos[i-1];
    }
    int ans = -1;
    for (int i = 0; i <= 100001; i++) {
        if (imos[i] >= i) ans = max(i, ans);
    }
    cout << ans-1 << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define int long long
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int imos[100005];

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

    rep(i, N) {
        int a, b; cin >> a >> b;

        imos[a]++, imos[b + 1]--;
    }

    rep(i, 100005) if (i) imos[i] += imos[i - 1];

    int ma = 1;

    rep(i, 100005) {
        if (imos[i] >= i) ma = i;
    }

    cout << ma - 1 << endl;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

int n;

bool check(VVI& table, int number) {
    // number ?????????????????????
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (table[i][0] <= number && table[i][1] >= number) {
            count++;
        }
    }
    if (count >= number - 1) {
        return true;
    } else {
        return false;
    }
}

void bs(VVI& table) {
    int left = 1;
    int right = n + 2;

    while (right - left > 1) {
        int middle = (left + right) / 2;
        if (check(table, middle) == false) {
            right = middle;
        } else {
            left = middle;
        }
    }
    cout << left - 1 << endl;
    return;
}

int main(void) {
    cin >> n;
    VVI table(n, VI(2));
    for (int i = 0; i < n; i++) {
        cin >> table[i][0] >> table[i][1];
    }
    bs(table);
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio> 
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)

int canGo[100001] = {0};

class Solve {
public:
	Solve() {};
	~Solve() {};

	void solver() {
		int ans = 0;
		int n;
		std::cin >> n;
		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));
		rep(i, n) {
			std::cin >> friends[i].first >> friends[i].second;
			//Exclude myself
			friends[i].first--; friends[i].second--;
		}
		//Record the number of people who can go.
		for (auto& x : friends) {
			canGo[x.first]++;
			canGo[x.second]--;
		}
		//fill
		rangeRep(i, 1, n + 1) {
			canGo[i] += canGo[i - 1];
		}
		int max = 0;
		rep(i, n+1) {
			if (canGo[i] >= i) {
				max = i;
			}
		}
		std::cout << max << std::endl;
	}
private:

};

int main() {
	Solve solve;
	solve.solver();
	return 0;
}

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

#define M 100002
int table[M];

int main(){
  int N;
  int a, b;

  cin >> N;
  for(int i = 0; i < N; i++){
    cin >> a >> b;
    table[a] += 1;
    table[b+1] += -1;
  }
  /*for(int i = 0; i < 10; i++){
    cout << table[i] << " ";
  } cout << endl;
  */for(int i = 1; i < M; i++){
      table[i] += table[i-1];
  }
  /*for(int i = 0; i < 10; i++){
    cout << table[i] << " ";
  } cout << endl;
  */
  int max = 0;
  for(int i = 0; i < M; i++){
    if(max < table[i]) max = table[i];
  }
  /*for(int i = 0; i < 10; i++){
    cout << table[i] << " ";
  }cout << endl;
  */
  cout << max-1 << endl;
}