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>
#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>=2;i--)
    if(d[i]>=min(n,i))ans=max(ans,i);
  cout<<ans-1<<endl;
  return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const static int tx[] = {0,1,1,1,0,-1,-1,-1};
const static int ty[] = {-1,-1,0,1,1,1,0,-1};
 
static const double EPS = 1e-8;

int main(){
  int N;
  while(~scanf("%d",&N)){
    int dp[100100];
    memset(dp,0,sizeof(dp));

    for(int i=0;i<N;i++){
      int lower,upper;
      scanf("%d %d",&lower,&upper);
      dp[lower - 1]++;
      dp[upper]--;
    }

    int res = 0;
    int sum = 0;
    for(int participants=0;participants<=100001;participants++){
      sum += dp[participants];
      if(sum >= participants) res = participants;
    }
    printf("%d\n",res);
  }
}

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

using namespace std;

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

	vector< pair<int,int> > data(N);
	int nmin = 1000001;
	int nmax = 0;
	for (int i = 0; i < N; ++i) {
		cin >> data[i].first >> data[i].second;
		nmin = min(nmin,data[i].first);
		nmax = max(nmax,data[i].second);
	}
	nmax = min(N+1,nmax);

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

	bool find = false;
	int res = 0;
	for (int i = nmax; !find && i >= nmin; --i) {
		int count = 0;
		for (int j = 0; j < N; ++j) {
			if (data[j].first <= i && data[j].second >= i) {
				++count;
			}
		}
		if (count >= i-1) {
			find = true;
			res = i-1;
		}
	}
	cout << res << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int main() {
	int n;	cin >> n;
	vint a(n, 0), b(n, 0);
	rep(i, n)	cin >> a[i] >> b[i];
	sort(b.begin(), b.end());
	//b[0]??????????????????????????§????????¨??????????????????????????§??????
	sort(a.begin(), a.end());
	if (b[0] >= a[n - 1])	b[0] = 1;
	cout << b[0] - 1 << endl;
	return 0;
}

C++: #include<iostream>
#include<climits>
int main()
{
	int imos[100003];
	for(int i = 0; i < 100003; i++)
	{
		imos[i] = 0;
	}
	int N;
	std::cin >> N;
	for(int i = 0; i < N; i++)
	{
		int a, b;
		std::cin >> a >> b;
		imos[a]++;
		imos[b + 1]--;
	}
	for(int i = 0; i < 100002; i++)
	{
		imos[i + 1] += imos[i];
	}
	int ninzu_max = INT_MIN;
	for(int i = 0; i < 100003; i++)
	{
		if(i - 1 <= imos[i])
		{
			ninzu_max = i;
		}
	}
	std::cout << ninzu_max - 1 << std::endl;
	return 0;
}

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<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
int main(){
    vector<P> allow;
    int n;
    cin >> n;
    int tmp_min = INF;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        a--; //b--;
        tmp_min = min(tmp_min,min(a,b));
        allow.push_back(P(a,b));
    }
    int tmp_max = 0;
    int ans = 0;
    for(int i=tmp_min; i<=n; i++){
        int cnt = 0;
        rep(j,n){
            if(i >= allow[j].first && i<= allow[j].second) cnt++;
            //cout << cnt << endl;
        }
        if(tmp_max <= cnt && i < cnt) {
            tmp_max = cnt;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int N,a,b,ans;
  int max=0;
  int man[100001];
  for(int i=0;i<100001;i++){
    man[i]=0;
  }
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++) man[j]++;
  }
  for(int i=0;i<N;i++){
    if(man[i]>max && man[i]>=i-1){
      max=man[i];
      ans=i;
    }
  }
  cout<<ans<<endl;
  return 0;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int i;
	int N=read();
	int a[100002]={0},b[100002]={0};
	int sum[N+1]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()] --;
	}
	
	for(i=1;i<=N;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=N;i>=0;i--){
		if(sum[i]>=i){
			cout << i-1 << endl;
			return 0;
		}
	}
	cout << 0 << endl;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
int main(){
    vector<P> allow;
    int n;
    cin >> n;
    int tmp_min = INF;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        a--; b--;
        tmp_min = min(tmp_min,min(a,b));
        allow.push_back(P(a,b));
    }
    sort(allow.begin(),allow.end());
    int tmp_max = 0;
    int ans = 0;
    for(int i=tmp_min; i<=n+1; i++){
        int cnt = 0;
        rep(j,n){
            if(i >= allow[j].first && i<= allow[j].second) cnt++;
            //cout << cnt << endl;
        }
        if(tmp_max <= cnt && i <= cnt) {
            tmp_max = cnt;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}