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>
typedef long long ll;
#define REP(i,n) for(int i = 0;i < (n);i++)
#define rep(i,m,n) for(int i = (m);i < (n);i++)
#define P pair<int,int>
#define pb push_back
#define mk make_pair
using namespace std;
#define Vec(a) vector <int> a
const int INF = 1 << 20;
const int MOD = 1e9+7;





int main(){
  int n;
  cin >> n;
  int table[100002] = {0};
  
  REP(i,n){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    table[a]++;
    table[b+1]--;
  }

  // シミュレート
  for (int i = 0; i < 100002; i++){
    if (0 < i) table[i] += table[i - 1];
  }
  // 最大値を調べる
  int M = 0;
  for (int i = 1; i <= n; i++) {
    if(table[i] >= i){
      M = max(i,M);
    }
    else{
      //M = max(table[i],M);
    }
  }
  cout << M << endl;
  return 0;
}

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

int go[100005] = {};

int main(){
    int n, a, b;
    cin >> n;

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

    int ans = 0, sum = 1;
    for(int i = 2; i < 100005; i++){
        sum += go[i];
        if(sum >= i)    ans = i;
    }

    cout << max(0, ans-1) << endl;

    return 0;
}

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

//define
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1)cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1)cout<<#x<<":"<<x<<endl;
#define ll long long

//typedef
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

bool possible(int num, vector<int> &a, vector<int> &b) {
	int cnt = 0;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] <= num && num <= b[i]) {
			cnt++;
		}
	}
	if (cnt >= num)
		return true;
	else
		return false;

}

int main(){
	int n;
	cin >> n;
		vector<int> a(n), b(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		int lb = 0;
		int ub = n+1 ;
		while (ub - lb > 1) {
			int mid = (ub + lb) / 2;
			if (possible(mid,a,b)) {
				lb = mid;
			}
			else {
				ub = mid;
			}
		}
		cout << lb << endl;


	return 0;
}

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

const int MAX_N = 100000;
int table[MAX_N + 2];
int n;

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int p, q;
		scanf("%d %d", &p, &q);
		int t = table[p];
		for(int j = p; j <= q; j++)
			table[j]++;
	}
	int res = 0;
	for(int i = 0; i <= n + 1; i++){
		if(table[i] + 1 >= i){
			res = i - 1;
		}
	}
	printf("%d\n", res);
	return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<int> V(100005);
    V[0]++;
    for (int i = 0; i < N; i++) {
        int a, b; cin >> a >> b;
        V[a]++;
        V[b+1]--;
    }

    int res = 0;
    for (int i = 0; i <= 100001; i++) {
        V[i + 1] += V[i];
        if (i <= V[i]) res = i;
    }

    cout << res - 1 << endl;

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
using namespace std;
int main(int argc, char *argv[])
{
  for(;;) {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<pair<int,int>> abs;
    for(int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      abs.push_back(make_pair(a, b));
    }
    int best = 0;
    vector<int> cnt(100003, 0);
    cnt[0] = 1;
    cnt[n+2] = -1;
    for(int i = 0; i < n; i++) {  // differences of the histogram
      cnt[abs[i].first]++;
      cnt[abs[i].second+1]--;
    }
    for(int i = 1; i <= n + 1; i++) { // build the histogram
      cnt[i] += cnt[i-1];
    }
    for(int i = 1; i <= n + 1; i++) {
      if(cnt[i] >= i) best = i - 1;
    }
    
    /* This is a bit more efficient version.
    int s = 0;
    sort(abs.begin(), abs.end());
    std::priority_queue<int> heap;
    // heap = friends x s.t. abs[x].second >= k+1
    for(int k = 1; k <= n; k++) {
      while(s < n && abs[s].first <= k+1) {
        heap.push(-abs[s].second);
        s++;
      }
      while(heap.size() > 0 && -heap.top() <= k) heap.pop();
      if(heap.size() >= k) { best = k; }
    }
    */
    /* This is a naive version.
    int s = 0;
    sort(abs.begin(), abs.end());
    for(int k = 1; k <= n; k++) {
      while(s < n && abs[s].first <= k+1) s++;
      int c = k;
      for(int j = s - 1; c > 0 && j >= 0; j--) {
        if(abs[j].second >= k+1) c--;
      }
      if(c == 0) { best = k; }
    }
     */
    cout << best << endl;
    break;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
 
typedef long long ll;
typedef pair<int, int> ii;

int main() {
	
	ll n;
	cin >> n;
	map<int, int> mp;
	rep(i, n){
		ll a, b;
		cin >> a >> b;
		mp[a]++;
		mp[b] += 0;
		mp[b+1]--;
	}
	
	int ans = 0;
	int tmp = 0;
	for(auto it : mp){
		tmp += it.second;
		if(tmp >= it.first-1) ans = max(ans, it.first-1);
	}
	
	cout << ans << endl;
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <array>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>

#define MAX 100005
using namespace std;


int main(void)
{
	int table[MAX];

	int i, j;
	int n;
	int temp1, temp2;
	int max = 0;
	int count = 0;
	bool ha[MAX];

	cin >> n;

	vector <pair<int, int>> a;

	for (i = 0; i < MAX; i++) {
		table[i] = 0;
	}

	for (i = 0; i < n; i++) {
		cin >> temp1 >> temp2;
		a.push_back(make_pair(temp1, temp2));


	}

	sort(a.begin(), a.end());



	for (i = 0; i <= n; i++) { //?????????????????°i????¢???????????????????????????????????????????
		count = 0;

		for (j = 0; j < n; j++) {
			if (a[j].first <= i + 1 && i + 1 <= a[j].second) {
				count++;
			}

			if (count == i) {
				max = i;
				break;
			}
		}

	}



	cout << max << endl;



	return (0);
}