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 <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;
}

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

int a[100005] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c-1]++;a[b]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100002;
  int n, a, b;
  int dp[MAX+1] = {};
  
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=0; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
  
  int mx = 0;
  for(int i=0; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int cnt[100002];
int main() {
	int n; scanf("%d", &n);
	rep(i, n) {
		int a, b;
		scanf("%d%d", &a, &b);
		cnt[a - 1]++; cnt[b]--;
	}
	int ans = 0;
	for (int i = 0; i <= n; i++) {
		if (i)cnt[i] += cnt[i - 1];
		if (cnt[i] >= i)ans = i;
	}
	printf("%d\n", ans);
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v(100000 + 2, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        int b;
        cin >> a >> b;
        for (int j = a; j <= b; ++j) {
            v[j] += 1;
        }
    }
    int max_{0};
    //for(auto&& i : v) {
    //    if(i) {
    //        cout << i << endl;
    //    }

    //}
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] != 0 && (i - 1) <= v[i]) {
            max_ = i;
        }
    }
    if(max_ != 0) {
        cout << (max_ - 1) << endl;
    } else {
        cout << max_ << endl;
    }
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define MP make_pair
#define PII pair<int,int>

int main()
{
	int N;
	int ans = 0;
	cin >> N;
	vector <PII> friends;
	queue <PII> q;
	REP(i, N)
	{
		int t1, t2;
		cin >> t1 >> t2;
		friends.push_back(MP(t1, t2));
	}
	sort(friends.begin(), friends.end());
	REP(i, N)
	{
		if (q.empty())
		{
			q.push(friends[i]);
			ans = max(ans, int(q.size()));
		}
		else if (friends[i].first > N + 1)continue;
		else if (q.front().second < friends[i].first)
		{
			while (1)
			{
				if (q.empty())break;
				if (q.front().second >= friends[i].first)break;
				q.pop();
			}
			q.push(friends[i]);
		}
		else
		{
			q.push(friends[i]);
			ans = max(ans, int(q.size()));
		}
	}
	cout << ans - 1 << endl;
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Sum
{
    int n;
    vector<int> data;
    void query(int a, int b, int k, int l, int r, int x){
        if(r < a || b < l)
            return;
        if(a <= l && r <= b)
            data[k] += x;
        else{
            query(a, b, k*2+1, l, (l+r)/2, x);
            query(a, b, k*2+2, (l+r+1)/2, r, x);
        }
    }
public:
    Sum(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, 0);
    }
    void add(int a, int b, int x){
        query(a, b, 0, 0, n-1, x);
    }
    int get(int k){
        k += n-1;
        int ret = data[k];
        while(k > 0){
            k = (k - 1) / 2;
            ret += data[k];
        }
        return ret;
    }
};

int main()
{
    int n;
    cin >> n;

    Sum sum(n+2);
    for(int i=0; i<n; ++i){
        int a, b;
        cin >> a >> b;
        sum.add(a, b, 1);
    }

    int i = n;
    for(;;){
        if(sum.get(i+1) >= i){
            cout << i << endl;
            break;
        }
        -- i;
    }

    return 0;
}