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
#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;
		vector<int> a(n), b(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		int lb = 0;
		int ub = n ;
		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<iostream>
#include<vector>
using namespace std;

vector<pair<int, int>> ab(100002);
vector<int> k(100002);

int main() {
	int N;
	cin >> N;
	for (int i = 0; i < 100002; i++) {
		ab[i].first = 0; 
		ab[i].second = 0;
}

	for (int i = 1; i <= N; i++) {
		cin >> ab[i].first >> ab[i].second;
		k[ab[i].first]++;
		k[ab[i].second]--;
	}

	int sum = 0;
	int ans = 0;

	for (int i = 1; i < 100002; i++) {
		sum += k[i];
		if (sum + 1 >= i) {
			ans = i;
		}
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
int a[100005];
int b[100005];
int imos[100005];
bool ok(int m) { return imos[m] >= m; }
int main() {
    cin >> N;
    rep(i, N) cin >> a[i] >> b[i];
    rep(i, N) a[i]--, b[i]--;
    rep(i, N) {
        ++imos[a[i]];
        --imos[b[i] + 1];
    }
    rep(i, 100005) if (i) imos[i] += imos[i - 1];
    // rep(i, N + 1) cout << i << ", " << imos[i] << endl;
    int ans = 0;
    rep(i, N + 1) if (ok(i)) ans = i;
    cout << ans << endl;
    return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<int> V(100005);
    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 <= N; i++) {
        V[i + 1] += V[i];
        if (i <= V[i] + 1) res = i;
    }

    cout << res - 1 << endl;

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Data{int num,io;};
bool operator<(Data a,Data b){return a.num!=b.num?a.num<b.num:a.io>b.io;};
enum{IN=1,OUT=-1};

int main()
{
  int N,a,b;
  cin>>N;
  vector<Data>list;
  for(int i=0;i<N;i++){
    cin>>a>>b;
    Data d1={a,IN};
    list.push_back(d1);
    Data d2={b,OUT};
    list.push_back(d2);
  }
  sort(list.begin(),list.end());
  int sum=1,ans=0,prev=0;
  for(int i=0;i<N*2;i++){
    if(sum>=list[i].num)ans=list[i].num-1;
    else if(sum>=prev)ans=max(ans,sum-1);
    sum+=list[i].io;
    prev=list[i].num;
  }
  cout<<ans<<endl;
}

C++: #include <iostream>

using namespace std;

int mymax(int a, int b){
  return (a > b) ? a : b;
}


int main(void){
  int N;
  cin >> N;

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

  int ans[N+2];
  for(int i = 0 ; i < N + 2 ; i++){
    ans[i] = 1;
  }

  for(int i = 0 ; i < N ; i++){
    for(int j = a[i] ; j <= b[i] && j < N + 2 ; j++){
      if(ans[j] < j){
        ans[j]++;
      }else{}
    }
  }

  int max = -1;
  for(int i = 0 ; i < N + 2 ; i++){
    if(max < ans[i] && i <= ans[i]){
      max = ans[i];
    }
  }

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

}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100002

int main() {
	int imos[MAX] = {};
	int N; cin >> N;

	for (int i = 0; i < N; i++) {
		int a, b; cin >> a >> b;
		imos[a]++; imos[b + 1]--;
	}
	imos[0] = 1;
	for (int i = 1; i < MAX; i++) {
		imos[i] += imos[i - 1];
	}
	int ans = 0;
	for (int i = 0; i < MAX; i++) {
		if (imos[i] >= i)ans = max(ans, i - 1);
	}
	cout << ans << endl;
	return 0;
}

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

#define DEBUG

#define MAX 100000

using namespace std;


int main(void)
{
  int N;
  cin >> N;
  int a[N], b[N];
  
  //vector<piar<int, int> > v;
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i];
    //int a, b;
    //cin >> a >> b;
    //v.push_back(make_pair(a,b));
  }

  for (int i = N+1; i >= 2; i--) {
    int f = 0;
    for (int j = 0; j < N; j++) {
      if (a[j] <= i && i <= b[j])
	f++;
    }
    if (f >= i-1) {
      cout << i-1<< endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

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

int main() {
    int n;
    std::cin >> n;
    std::vector<std::pair<int,int>> xs(n);
    for(int i = 0; i < n; ++i) {
        std::cin >> xs[i].first >> xs[i].second;
    }
    std::vector<int> ys(200000);
    for(int i = 0; i < n; ++i) {
        ys[xs[i].first] += 1;
        ys[xs[i].second+1] -= 1;
    }
    int t = 0;
    std::vector<int> zs;
    for(int i = 0; i < 150000; ++i) {
        t += ys[i];
        zs.push_back(t);
    }
    int res = 0;
    for(int i = 0; i < 150000; ++i) {
        if (zs[i] >= i - 1) {
            res = i - 1;
        }
    }
    printf("%d\n", res);
}

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;


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 res = 0;
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	for (int i = 0; i <= n; i++) {
		int u=a.end()-upper_bound(a.begin(), a.end(), i+1);
		auto it = lower_bound(b.begin(), b.end(), i+1);
		int l = it - b.begin();
		if (n - l - u>=i)
			res = max(i, res);
	}
	cout << res << endl;
	return 0;
}