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 <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--;
        table[start]++;
        table[end]--;
    }

    for (int i = 1; i <= MAX; i++) {
        table[i] += table[i - 1];
    }

    for (int i = MAX; i >= 0; i--) {
        if (i <= table[i]) {
            cout << i << endl;
            break;
        }
    }
    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, i-1);
        }
    }
    cout<< s<< endl;

    return 0;
}

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

int main(){
    int n;  scanf("%d",&n);
    int OK[n][2];
    for(int i=0;i<n;i++){
        scanf("%d",&OK[i][0]);
        scanf("%d",&OK[i][1]);
    }

    int max=0;
    
    int result[n+2];  for(int i=0;i<n+2;i++)result[i]=0;

    for(int i=1;i<=n+1;i++){ // i は（私を含め）行く人数
        for(int j=0;j<n;j++){
            if(OK[j][0]<=i && i<=OK[j][1])result[i]++;
        }

        if(i<=result[i])result[i]=i-1;
        else if(i-1>result[i])result[i]=-1;

        if(result[i]>max)max=result[i];
    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

//    for(int i=0;i<n+2;i++){
//        if(i<=result[i])result[i]=i-1;
//        else if(i-1>result[i])result[i]=-1;
//    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

//    int max=0;
//    for(int i=0;i<n+2;i++){
//        if(result[i]>max)max=result[i];
//    }

    cout<<max<<endl;


    return 0;
}

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

using namespace std;

int N, a[100010], b[100010];

bool check(int m) {
    int cnt = 0;
    for (int i = 0; i < N; i++)
        if (a[i] <= m && m <= b[i]) cnt++;
    
    if (cnt + 1 < m) return false;
    else return true;
}

int main(void) {
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> a[i] >> b[i];
    
    int l = 2, r = N + 2;
    while (r - l > 1) {
        int m = (l + r) / 2;
        if (check(m)) l = m;
        else r = m;
    }
    if (l == 2) l = 1;
    cout << l - 1 << endl;

    return 0;
}

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

int N;
int imosu[IMOSU_MAX];
int a,b;
int ans;

int main() {
	cin>>N;
	for (int i = 0; i < N; i++) {
		cin>>a>>b;
		imosu[a]++;
		imosu[b+1]--;
	}
	imosu[0]++;
	for (int i = 1; i < IMOSU_MAX; i++) {
		imosu[i]+=imosu[i-1];
		if(i>=ans && imosu[i]>=i)ans=i;
	}
	//for(int i=0;i<10;i++)cout<<" "<<imosu[i];
	cout<<ans-1<<endl;
	//cin>>N;
	return 0;
}

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

using namespace std;

int main()
{
	array<int, 100000> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		ans = max(ans, friends[i]);
	}

	if(n == 1){
		ans ++;
	}
	ans = max(ans - 2, 0);
	cout << ans << endl;

}

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

int N;
int imosu[IMOSU_MAX];
int a,b;
int ans;

int main() {
	cin>>N;
	for (int i = 0; i < N; i++) {
		cin>>a>>b;
		imosu[a]++;
		imosu[b]--
		;
	}
	imosu[0]++;
	for (int i = 1; i < IMOSU_MAX; i++) {
		imosu[i]+=imosu[i-1];
		if(imosu[i]>ans && imosu[i]==i)ans=imosu[i];
	}
	cout<<ans-1<<endl;
	cin>>N;
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;
int main(void){
	int n;
	cin>>n;
	int imos[100010]={};
	int cum[100010]={};
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		imos[a]++;
		imos[b+1]--;
	}
	cum[0]=imos[0];
	for(int i=1;i<100010;i++){
		cum[i]=cum[i-1]+imos[i];
	}
	int result=0;
	for(int p=0;p<=n;p++){
		if(cum[p+1]>=p){
			result=p;
		}
	}
	cout<<result<<endl;
	return 0;
}

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

#define int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1e18+1;

signed main() {
	int n;
	int imos[100005] = {0};

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

	for ( int i = 1; i <= 100003; i++ ) {
		imos[i] = imos[i]+imos[i-1];
	}

	//for ( int i = 0; i < 7; i++ ) cout << imos[i] << " ";
	//cout << endl;

	for ( int i = 100004; i >= 0; i-- ) {
		if ( i <= imos[i]+1 ) {
			cout << i-1 << endl;
			break;
		}
	}
	return 0;
}

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

using namespace std;

int main()
{
	array<int, 100002> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	//cout << 1 << endl;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		if(friends[i] >= ans && friends[i] - i >= 0){
			ans = max(ans, friends[i]);
			if(ans - 1 == i)ans++;
		}
		//cout << friends[i] << endl;
	}

	//cout << ans << endl;
	ans = max(0, ans - 2);
	cout << ans << endl;
	return 0;
}