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 <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define lengthof(array) (sizeof(array) / sizeof(*array))
#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)
#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
	return os;
}

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

	const int MAX = 100001;
	vector<int> cnt(MAX + 2, 0);

	int n;
	cin >> n;

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

	for(int i = 0; i < MAX; ++i)
		cnt[i + 1] += cnt[i];

	for(int i = n + 1; i > 0; --i) {
		if(cnt[i] + 1 >= i) {
			cout << i - 1 << endl;
			return EXIT_SUCCESS;
		}
	}

	assert(false);
	return EXIT_SUCCESS;
}

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

using namespace std;

int n;

int main()
{
    cin >> n;
    vector< vector<long long> > ab(n + 2, vector<long long>(0) );
    vector<long long> away(n + 2, 0);
    int a,b;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        if(a < n + 2) ab[a].push_back(b);
    }
    sort(ab.begin(), ab.end());
    int count = 1;
    int m = 1;
    for(int j = 1; j < n + 2; j++){
        if(ab[j].size() == 0) continue;
        if(count + ab[j].size() < j){
            count = count + ab[j].size();
            for (int k = 1; k <= ab[j].size(); k++)
            {
                away[(int)ab[j][k - 1] + 1]++;
            }
            continue;
        }
        for(int k = 1; k <= ab[j].size(); k++){
            count++;
            count -= away[count];
            //cout << count << endl;
            m = max(m, count);
            away[(int)ab[j][k - 1] + 1]++;
        }
    }
    //for(int k = 0; k < n + 2; k++) cout << away[k] << endl;
    cout << m - 1 << endl;
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int,int>;
#define FOR(i,a,b) for(ll i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1000000007;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
int main(){
  int n; cin >> n;
  vi imos(1e5+3,0);
  REP(i,n){
    size_t tmp;
    cin >> tmp;
    imos[tmp]++;
    cin >> tmp;
    imos[tmp+1]--;
  }
  int sum = 0;
  int ans = 0;
  FOR(i,2,n+2) {
    sum += imos[i];
    if(i<=sum+1){
      ans = i-1;
    }
  }
  cout << ans << endl;
}

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

int range[MAX][2];

int search(int n) {
  int number = n + 1;
  int flag = 1;
  while (flag) {
    flag = 0;
    for (int i = 0; i < n; i++) {
      if (number < range[i][0] || range[i][1] < number) {
	flag = 1;
	break;
      }
    }
    if (!flag) return number - 1;
    number--;
    if (number == 1) return 0;
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> range[i][0] >> range[i][1];
  }
  
  int ans = search(n);
  cout << ans << endl;
    
  return 0;
}

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

/*
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=jp
 * test of c++
 */


template <typename T>
std::vector<std::vector<T>> read_input() {
    std::string input_str;

    std::getline(std::cin, input_str);
    int arr_size = std::stoi(input_str);
    std::vector<std::vector<T>> input_vec(arr_size, std::vector<int>(2, 0));

    for (size_t i = 0; i < arr_size; i++) {
        std::getline(std::cin, input_str);
        std::istringstream iss(input_str);
        T begin, end;
        iss >> begin >> end;

        // this is for myself
        begin -= 1;
        end -= 1;
        input_vec.at(i).at(0) = begin;
        input_vec.at(i).at(1) = end;
    }
    return input_vec;
}


bool search(std::vector<std::vector<int>> input_vec, int ans) {
    if (ans == 0) {
        return true;
    }
    int count = 0;
    for (int i = 0; i < input_vec.size(); i++) {
        std::vector<int> person = input_vec.at(i);
        if (person.at(0) <= ans && ans <= person.at(1)) {
            count++;
        }
        if (count == ans) {
            return true;
        }
    }
    return false;
}


int main(int argc, char const *argv[]) {
    std::vector<std::vector<int>> input_vec = read_input<int>();
    int ans;

    // num: #friend
    for (int num = 0; num < input_vec.size(); num++) {
        if (search(input_vec, num)) {
            ans = num;
        } else {
            break;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}