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<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define MP make_pair
#define PII pair<int,int>

int main()
{
	int N;
	int ans = 0;
	cin >> N;
	vector <PII> friends;
	queue <PII> q;
	REP(i, N)
	{
		int t1, t2;
		cin >> t1 >> t2;
		friends.push_back(MP(t1, t2));
	}
	sort(friends.begin(), friends.end());
	REP(i, N)
	{
		if (q.empty())
		{
			q.push(friends[i]);
			if (q.front().first - 1 <= q.size())
			{
				ans = max(ans, int(q.size()));
			}
		}
		else if (friends[i].first > N + 1)continue;
		else if (q.front().second < friends[i].first)
		{
			while (1)
			{
				if (q.empty())break;
				if (q.front().second >= friends[i].first)break;
				q.pop();
			}
			q.push(friends[i]);
		}
		else if (q.front().second == friends[i].first)
		{
			q.pop();
			q.push(friends[i]);
		}
		else
		{
			q.push(friends[i]);
			if (q.front().first - 1 <= q.size())
			{
				ans = max(ans, int(q.size()));
			}
		}
	}
	cout << ans << endl;
	return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define int long long
typedef pair<int,int> Pint;

//-----------------------------------------------------------------------

const int MAX=100001;
int sum[MAX+1];

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

    int n; cin>>n;
    REP(i,n){
        int a,b; cin>>a>>b;
        sum[a]++; sum[b+1]--;
    }
    REP(i,MAX) sum[i+1]+=sum[i];

    int ans=0;
    REP(i,MAX+1){
        if(sum[i]>=i-1) ans=i-1;
    }
    cout<<ans<<endl;

}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define int long long
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int imos[100010];

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

    imos[0] = 1;

    rep(i, N) {
        int a, b;
        cin >> a >> b;

        imos[a]++, imos[b + 1]--;
    }

    rep1(i, 100005) imos[i] += imos[i - 1];

    int ma = 0;

    rep(i, 100005) if (imos[i] >= i) ma = i;

    cout << ma - 1 << endl;
}

C++: #include<cstdio>

using namespace std;

const int MAX = 100005;
int dp[MAX];

int main(){

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

  while(n--){
    int a,b;
    scanf("%d %d",&a,&b);
    for(int i = a; i <= b; i++) dp[i]++;
  }

  for(int i = MAX-1; i>0; i--)
    if(dp[i] >= i-1){
      printf("%d\n",i-1);
      break;
    }
  return 0;
}

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

int main(){
  int N, a, b, ans, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  ans = 0;
  for (int i = N+1; i > 0; --i) {
    t = 1;
    for (int j = 0; j < N; ++j) {
      if (i >= A[j] && i <= B[j]) t++;
    }
    if (t >= i) ans = max(ans, i-1);
  }
  cout << ans << endl;
}