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  INF 1100000000
#define	 MOD 1000000007
using namespace std;

int main(){
	int N;
	cin >> N;
	int members[110000];
	for(int i=0;i<110000;i++){
		members[i] = 0;
	}
	members[0]++;//私
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		members[a]++;
		members[b+1]--;
	}
	int max = members[0];
	for(int i=1;i<=N+1;i++){
		members[i] += members[i-1];
		if(members[i] >= i){
			max = i;
		}
	}
	
	max--;
	
	cout << max << endl;
	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]--;
    }
    come[0]++;
    come[100100-1]--;
    int ans = 0;
    int sum = 0;
    for(int i=0; i<100100; ++i) {
        sum += come[i];
        if(i<=sum) ans = i;
    }
    cout << ans-1 << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF (1<<28)
#define N (1<<17)

int h[100010];
int main(){
	int n; cin >> n;
	
	for(int i = 0 ; i < n ; i++){
		int a,b;
		cin >> a >> b;
		h[a]++;
		h[b+1]--;
	}
	int ans = 0 , cur = 0;
	for(int i = 1 ; i <= 100002 ; i++){
		cur += h[i];
		if(cur >= i) ans = max(ans,i-1);
	}
	cout << ans << endl;
	
}

C++: #include<iostream>

using namespace std;

int r[100100];
int main(){

    int N;//<=10^5
    cin>> N;
    int a[N], b[N];
    for(int i=0; i<N; i++) cin>> a[i]>> b[i];

    for(int i=0; i<N; i++){
        r[a[i]]++;
        r[b[i]+1]--;
    }
    for(int i=0; i+1<100100; i++){
        r[i+1]+=r[i];
    }
    int s=0;
    for(int i=0; i<100100; i++){
        if(r[i]>=i) s=i;
    }

    cout<< s-1<< 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;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

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;

#define MAX 1e05+1

int main(void) {
    int n;
    cin >> n;
    int start;
    int end;
    VI table(MAX + 1, 0);

    for (int i = 0; i < n; i++) {
        cin >> start >> end;
        start--;
        end--;
        for (int j = start; j <= end; j++) {
            table[j]++;
        }
    }

    for (int i = MAX; i >= 0; i--) {
        if (i <= table[i]) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
  int N, num[100010] = {0};
  cin >> N;
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    num[a]++, num[b+1]--;
  }
  partial_sum(num, num + 100010, num);
  int ans = 0;
  for(int i = 1; i <= N; i++){
    if(i <= num[i+1]) ans = max(ans, i);
  }
  cout << ans << endl;
  return 0;
}