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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;

template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);i++)
#define rep0(i,n) for(i=0;i<(n);i++)
#define repn(i,m,n) for(int i=(m);i<=(n);i++)
#define all(n) n.begin(),n.end()
#define mp make_pair
#define PUTLINE cout<<__LINE__<<endl;

int dx[]={1,-1,0,0,1,1,-1,-1,0};
int dy[]={0,0,1,-1,1,-1,1,-1,0};

int k[100100];

int main() {
	int n=in();
	rep(i,n){
		++k[in()-1];
		--k[in()];
	}
	int res,t=0;
	rep(i,100100){
		t+=k[i];
		if(i<=t)res=i;
	}
	cout<<res<<endl;
	return 0;
}

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

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int Digit[100002];
	for(int i = 0; i < 100002; ++i)
	{
		Digit[i] = 0;
	}
	int N;
	cin >> N;
	vector<P> Person(N);
	for(int i = 0; i < N; ++i)
	{
		cin >> Person[i].first >> Person[i].second;
	}
	for(int i = 0; i < N; ++i)
	{
		for(int j = Person[i].first; j <= Person[i].second; ++j)
		{
			++Digit[j];
		}
	}
	int Max = 1;
	for(int i = 2; i <= N; ++i)
	{
		if(Digit[i] >= i)
		{
			Max = i;
		}
	}
	cout << Max - 1 << endl;
}

int main()
{
	solve();
	return(0);
}

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000

int n;
int a[111111],b[111111];

int t[111111];

int main(){
	cin.sync_with_stdio(false);
	cin>>n;
	memset(t,0,sizeof(t));
	rep(i,n){
		int a,b;
		cin>>a>>b;
		a--; b--;
		t[a]++;t[b+1]--;
	}
	int res=0;
	rep(i,n+1){
		t[i+1]+=t[i];
		if(i+1<=t[i+1])maxch(res,i+1);
	}
	cout<<res<<endl;
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//sort

int main(void){
	int N;
	cin>>N;
	int a[100001],b[100001];
	rep(i,N)cin>>a[i]>>b[i];
	sort(a,a+N);
	sort(b,b+N);
	int current=0,max=0;
	int acnt=0,bcnt=0;//N??§?????????
	int begin=-1;
	while(1){
		if(acnt<=N-1){
			if(a[acnt]<b[bcnt]){
				current++;
				begin=a[acnt];
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				acnt++;
			}else if(a[acnt]>b[bcnt]){
				current--;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				bcnt++;
			}else{
				acnt++;
				bcnt++;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
			}
		}else{
			current--;
			if(current+1>=begin){
				if(current+1>b[bcnt]){
					if(b[bcnt]-1>max)max=b[bcnt]-1;
				}else{
					if(current>max)max=current;
				}
			}
			bcnt++;
		}
		if(acnt==N && bcnt==N)break;
	}
	cout<<max<<endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long

const int MAX = 100001;

int n;
int a[MAX], b[MAX];

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int imos[MAX + 2];
  fill(imos, imos + MAX + 2, 0);
  for (int i = 0; i < n; i++) {
    imos[a[i]]++;
    imos[b[i] + 1]--;
  }
  for (int i = 1; i < n + 2; i++) {
    imos[i] += imos[i - 1];
  }
  for (int i = n + 1; i >= 2; i--) {
    if (imos[i] >= i) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

int c[100100];

int main(){

    int N;
    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++){
        c[a[i]]++;
        c[b[i]+1]--;
    }
    for(int i=1; i<100100; i++){
        c[i]+=c[i-1];
    }
    int s=0;
    for(int i=1; i<100100; i++){
        if(c[i]>=i){
            s=max(s, c[i]-1);
        }
    }
    cout<< s<< endl;

    return 0;
}