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>
 
using namespace std;
 
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    Int(n);
    vi ans(100003, 0);
    ans[0]++;
    rep(i, n) {
        Int(a);
        Int(b);
        ans[a]++;
        ans[b+1]--;
    }
    int ret = 0;
    Rep(i, n+1) {
        ans[i] += ans[i-1];
        if (ans[i] >= i) {
            ret = i;
        }
    }
    cout << ret -1 << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int data[N];

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

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[j]++;
	}

	//debug
	//for(int i = 1; i < 10; i++) cout << "arr = " << data[i] << endl;

	int ans = 0;
	for(int i = 1; i <= n; i++){
		int ok = data[i];
		if(ok + 1 >= i) ans = max(ans, i);
	}

	cout << ans - 1 << endl;

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    int s[100002] = {};
    for (int i = 0; i < n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        for (int j = a; j <= b; ++j)
            ++s[j];
    }

    /*
    for (int i = 0; i <= n + 1; ++i)
        printf("%6d: %d\n", i, s[i]);
    */

    int ans = 0;
    for (int i = 0; i <= n + 1; ++i)
        if (i - 1 <= s[i])
            ans = i - 1;
    printf("%d\n", ans);

    return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100011] = {0}, ans = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            count[a]++;
            count[b + 1]--;
        }
        rep(i,n + 1) count[i + 1]+=count[i];
        rep(i,n + 1){
            if(count[i] >= i) ans = i;
        }
        cout << ans << endl;
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int main(){
	int n;
	int a[100001], b[100001];

	a[0] = 0; b[0] = 100001;
	cin >> n;
	int max = 0, min = 100001;
	for (int i = 0; i < n; ++i){
		cin >> a[i] >> b[i];
		if (a[i] <= min) min = a[i];
		if (b[i] >= max) max = b[i];
	}

	int max_friends = 0;
	for (int i = min; i < max+2; ++i){
		int friends = 0;
		for (int j = 0; j < n; ++j){
			if (a[j] <= i-1 && b[j] >= i-1){
				++friends;
	//			cout << i << a[j] << b[j] << "OK" << endl;
			}
			else{
		//		cout << i << a[j] << b[j] << "NG" << endl;

			}
		}

		if ((friends) == i-2){
			max_friends = i-2;
		}
	}

	cout << max_friends << endl;



	return 0;
}

C++: #include<bits/stdc++.h>
#define N 100002
using namespace std;
int n,a,b,d[N],ans;

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

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l]++;
        result[r + 1]--;
    }
    int ans = 0;
    for(int i = 2; i < n + 1; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] + 1 >= i) ans = max(i + 1, ans);
    }
    if(ans > 0) ans--;
    cout << ans << endl;
    return 0;
}

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

int table[100001][100001];

int main(){
  int N;
  int a, b;

  cin >> N;
  for(int i = 0; i < N; i++){
    cin >> a >> b;
    table[i][a-1] = 1;
    table[i][b] = -1;
  }
  for(int i = 0; i < N; i++){
    for(int j = 1; j < N; j++){
      table[i][j-1] += table[i][j];
    }
  }

  for(int i = 1; i < 100001; i++){
    for(int j = 0; j < N; j++){
      table[i][j] += table[i-1][j];
    }
  }
  int max = 0;
  for(int i = 0; i < 100001; i++){
    if(max < table[100000][i]) max = table[100000][i];
  }
  cout << max << endl;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,a,b,max=0,tmp;
  int imos[100003]={};
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> a >> b;
    imos[a]++;
    imos[b+1]--;
  }
  for(int i=1;i<=n+1;i++){
    imos[i]=imos[i]+imos[i-1];
  }
  for(int i=0;i<=n;i++){
    if(imos[i+1]<i) continue;
    if(imos[i+1]>i)imos[i+1]=i;
    max=(imos[i+1]<max)?max:imos[i+1];
  }
  cout << max<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

int main(void){
    int p;
    cin>>p;
    vi d(100002,0);
    rep(lp,p){
        int a,b; cin>>a>>b;
        d[a]++,d[b+1]--;
    }
    int ans=0;
    range(i,1,100002){
        d[i]+=d[i-1];
        if(d[i]>=i) ans=i;
    }
    cout<<ans-1<<endl;
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

#define abs(a) ((a) < (0)? -(a):(a))
#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)
#define pb push_back


int c[100010];


int main() {
    int n;
    cin >> n;
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        c[a-1]++;
        c[b]--;
    }
    int ans = 0, cur = 0;
    rep(i, 100001) {
        cur += c[i];
        if(cur >= i) ans = i;
    }
    cout << ans << endl;
}

C++: #include <iostream>
using namespace std;
 
int main()
{
  int N;
  cin >> N;
  int num[100010];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    ++num[a];
    --num[b+1];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 0; i <= N+1; i++) {
    acc += num[i];
    if (acc >= i-1) {
      ans = i-1;
    }
  }
  cout << ans << endl;
  return 0;
}