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 rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int c[100003];

int main() {

    int n;
    cin >> n;

    rep(i, n) {
        int a, b;
        cin >> a >> b;
        c[a]++;
        c[b+1]--;
    }

    int ans=0, t=1;
    rep(i, 100003) {
        t += c[i];
        if (i <= t) {
            ans = i;
        }
    }

    cout << ans-1 << endl;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) {return a = min(a, b);}
template <typename T> T &chmax(T &a, const T &b) {return a = max(a, b);}

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int N;
using pii = pair<int, int>;
int num[100005];

int main(){
	cin >> N;
	vector<pii> req; 
	REP(cnt, N) {
		int a, b;
		cin >> a >> b;
		num[a]++; num[b+1]--;
	}
	REP(i, N+1) num[i+1] += num[i];
	int res = 0;
	REP(i, N+1)???if(num[i]+1 > i) res = max(res, i);
	cout << max(res-1, 0) << endl;
	return 0;

}

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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	const int MAX = 100001;
	vector<int> cnt(MAX + 2, 0);

	int n;
	cin >> n;

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

	for(int i = 0; i < MAX; ++i)
		cnt[i + 1] += cnt[i];

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

	return EXIT_SUCCESS;
}

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

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

C++: #include <iostream>
#include <string.h>

int main(void){
	int n;
	std::cin >> n;
	int endmax = 0;

	int* pn = new int[100000];
	memset(pn, 0, 100000 * sizeof(int));

	for(int i=0; i<n; i++){
		int beg, end;
		std::cin >> beg >> end;
		for(int i = beg; i <= end; i++) pn[i]++;
		if(end > endmax) endmax = end;
	}

	int mps = 1;
	for(int i=1; i<=endmax; i++){\
		if(pn[i]+1 >= i)mps = i;
	}

	std::cout << (mps-1) << '\n';
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"

struct edge {
  int from;
  int to;
  ld cost;
};
bool operator<(const edge&l, const edge&r) {
  return l.cost < r.cost;
}
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};
int main() {
  
  int N; cin >> N;
  vector<int>nums(1000000);
  vector<int>sums(1000000);
  for (int i = 0; i < N; ++i) {
    int a, b; cin >> a >> b;
    nums[a-1]++;
    nums[b]--;
  }
  int asum = 0;
  for (int i = 1; i < 1000000; ++i) {
    asum += nums[i];
    sums[i] = asum;
  }
  int ans = 0;
  for (int i = 0; i < N + 2; ++i) {
    if (sums[i] >= i)ans = i;
  }
  cout << ans << endl;
  return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>

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

  int imos[100000+10]={};
  for (int i=0; i<N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    ++b;
    ++imos[a];
    --imos[b];
  }

  ++imos[1];
  --imos[100002];

  for (int i=1; i<=100001; ++i)
    imos[i+1] += imos[i];

  for (int i=N+2; i--;)
    if (i <= imos[i])
      return !printf("%d\n", i-1);

  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<(S)<<endl
#define ck(a,b) (0<=(a)&&(a)<(b))

using namespace std;

int main(){
	int N,ans=1;
	cin>>N;
	int A[100002]={0};
	A[2]++;
	REP(i,0,N){
		int a,b;
		cin>>a>>b;
		A[a]++,A[b+1]--;
	}
	REP(i,3,N+2){
		A[i]+=A[i-1];
	}
	for(int i=N+1;i>=2;i--){
		if(A[i]>=i){
			ans=i;
			break;
		}
	}
	p(ans-1);
	return 0;
}

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

const int MAXN = 100005;
int N;
int a[MAXN], b[MAXN];

int main() {
  while (cin >> N) {
    map<int, int> m;
    for (int i = 0; i < N; ++i) {
      cin >> a[i] >> b[i];
      m[a[i]]++;
      m[b[i]+1]--;
    }
    // 1 3   3 4
    // 1 +1
    // 3 +1
    // 4 -1
    // 5 -1
    int res = 1, num = 1, prev = 0;
    for (map<int, int>::iterator it = m.begin();
         it != m.end(); ++it) {
      if (num >= prev) {
        res = max(res, min(num, it->first - 1));
      }
      prev = it->first;
      num += it->second;
    }
    cout << res - 1 << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<functional>
using namespace std;

#define INF 1<<21
#define MOD 1000000007

int main(void) {
  int n;
  int a, b;
  cin >> n;
  int ans[100005];
  fill(ans, ans + 100005, 0);
  for (int i = 0; i < n; i++) {
	cin >> a >> b;
	ans[a]++;
	ans[b + 1]--;
  }

  int m = 0;
  for (int i = 1; i <= 100005; i++) {
	ans[i] += ans[i-1];
  }
  for (int i = 1; i <= 100005; i++) {
	if (ans[i+1] >=i) {
	  m = i;
	}
  }
  cout << m << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int d[100100];

signed main(){
    int n;
    cin >> n;
    rep(i, 0, n){
        int a, b;
        cin >> a >> b;
        d[a] ++;
        d[b + 1]--;
    }
    rep(i, 1, 100010){
        d[i] += d[i - 1];
    }
    int ans = 0;
    rep(i, 0, 100010){
        // cout << d[i] << " ";
        if(d[i] >= i - 1){
            ans = i - 1;
        }
    }
    cout << ans << endl;
}

C++:     #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
     
    int main() {
    	int N;
    	vector<int> F;
    	
    	cin>>N;
    	for(int i=0;i<N+1;i++){
    		F.push_back(0);
    	}
     
    	for(int i=0;i<N;i++){
    		int x,y;
    		cin>>x>>y;
     		
     		int xn=max(0,x-1);
    		F[xn]++;
    		if(y<=N){
    			F[y]--;
    		}
    	}
     
    	for(int i=0;i<N;i++){
    		F[i+1]+=F[i];
    	}
     
    	int ans;
    	for(int i=0;i<N+1;i++){
    		if(F[i]>=i){
    			ans=i;
    		}
    	}
     
    	cout<<ans<<endl;
     
    	return 0;
    }