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>
using namespace std;

int a[100000] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c]++;a[b+1]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//sort

int main(void){
	int N;
	cin>>N;
	int a[100001],b[100001];
	rep(i,N)cin>>a[i]>>b[i];
	sort(a,a+N);
	sort(b,b+N);
	int current=0,max=0;
	int acnt=0,bcnt=0;//N??§?????????
	int begin=-1;
	while(1){
		if(acnt<=N-1){
			if(a[acnt]<b[bcnt]){
				current++;
				begin=a[acnt];
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				acnt++;
			}else if(a[acnt]>b[bcnt]){
				current--;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				bcnt++;
			}else{
				acnt++;
				bcnt++;
			}
		}else{
			current--;
			if(current+1>=begin && current+1<=b[bcnt]){
				if(current>max)max=current;
			}
			bcnt++;
		}
		if(acnt==N && bcnt==N)break;
	}
	cout<<max<<endl;
	return 0;
}

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

int memo[100010];

int main(){
	int N;
	cin >> N;
	for(int i = 0 ; i < N ; i++){
		int a,b;
		cin >> a >> b;
		memo[a-1]++;
		memo[b]--;
	}
	int sum = 0;
	for(int i = 1 ; i <= 100001 ; i++){
		memo[i] += memo[i-1];
		//if( i < 10 ) cout << memo[i] << " ";
		
		if( i <= memo[i] ) sum = max(sum,i);
	}
	cout << sum << endl;
	
}

C++: #include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);

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

    vi sum(n + 2);
    rep(loop, n){
        int a, b; cin >> a >> b;
        chmin(b, n);
        sum[a]++, sum[b + 1]--;
    }

    int res = 0;
    rep(i, 1, n + 1){
        sum[i] += sum[i - 1];

        if(sum[i] >= i){
            chmax(res, i);
        }
    }
    res--;
    chmax(res, 0);

    cout << res << endl;

    return 0;
}

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // use key 100001

int n;
int np;
int min_;
int max_;

int solve() {
	cin >> n;
	np = n + 1;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		if (min_ > np) {
			continue;
		}
		box[min_ - 1]--;
		max_ = min(max_, np);
		box[max_]++;
	}
	box[np + 2] = 0;
	for (int i = np + 1; i >= 0; i--) {
		box[i] += box[i+1];
		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	std::ios::sync_with_stdio(false);
	std::cin.tie(false);


	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

P a[1000000];
int n;

int search(int m){
  int c=0;
  rep(i,n){
    if(a[i].first<=m&&m<=a[i].second)
      c++;
  }

  if(c>=m) return true;

  return false;
}


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

  int r=0,l=inf,m;

  while(l-r!=1){
    m = (r+l)/2;
    
    if(search(m))
      r = m;
    else
      l = m;
  }

  cout<<r<<endl;

}

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

#define PI 4*atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){
  int N;
  cin >> N;
  vector<int> c(N + 1, 0);
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    c[a - 1]++;
    c[b]--;
  }
  int sum = 0, ans = 0;
  for(int i = 1; i <= N; i++){
    sum += c[i];
    if(i <= sum)ans = i;
  }
  cout << ans << endl;
}

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

int imos[111111];
int main(){
    int n;
    while(cin >> n){
        memset(imos,0,sizeof(imos));
        for(int i=0;i<n;i++){
            int a,b;
            cin>>a>>b;
            imos[a]++;
            imos[b+1]--;
        }
        for(int i=1;i<111111;i++){
            imos[i]+=imos[i-1];
        }
        int ans=0;
        for(int i=0;i<111111;i++){
            if(imos[i]>=i)ans=max(ans,i-1);
        }
        cout << ans << endl;
    }
}