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 pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(1000010);
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    ++b;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,n){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,1,n+1){
    k = max(k,ans[i]); 
  }
  if(k == 1) cout << 0 << endl;
  else
    cout << k-1 << endl;
  return 0;
}

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

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

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

// const ll MAX = 20;
const ll MAX = 100010;

ll tb[MAX];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    ll n; cin >> n;
    for(int i=0;i<n;i++){
        ll a, b; cin >> a >> b;
        tb[a]++;
        tb[b+1]--;
    }

    ll cnt[MAX+1];
    cnt[0] = 0;
    for(int i=0;i<MAX-1;i++){
        cnt[i+1] = cnt[i] + tb[i+1];
    }
    /*
    for(int i=0;i<20;i++) cout << cnt[i];
    cout << endl;
    */
    ll ans = 0;
    for(ll i=0;i<MAX-1;i++){
        if(cnt[i] >= i - 1){
            // cout << i << endl;
            ans = max(ans, i - 1);
        }
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <sstream>

using namespace std;

int a[200000] = {};

int main(){
	
	int N;
	
	cin >> N;
	
	for(int i = 0; i < N; i++){
		int p, q;
		cin >> p >> q;
		a[p - 1]++;
		a[q]--;
	}
	
	int count = 0;
	int ans = 0;
	
	for(int i = 0; i < 200000; i++){
		count += a[i];
		if(count >= i){
			ans = i;
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

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 first fi;
#define second se;
using namespace std;
typedef pair<int, int> P;

int d[100010];

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

C++: #include<iostream>
#include<map>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
typedef pair<int,int> P;
int main(){
	int n;
	cin>>n;
	int ans=0;
	//int a[MAX],b[MAX];
	int a,b;
	vector<P> box;
	for(int i=0;i<n;i++){
		//cin>>a[i]>>b[i];
		cin>>a>>b;
		box.push_back(P(a,b));
	}
	sort(all(box));
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=box[j].first){
				vec.push_back(box[j].second);
				
				
			}else
			break;
		}
		//cout<<i<<endl;
		sort(all(vec));
		int t=0;
		for(int k=0;vec.size()>k;k++){
			if(i<=vec[k]){
			t++;
			//cout<<"------"<<endl;
			}
		}
		
		int s=(upper_bound(vec.begin(),vec.end(),i,greater<int>())-vec.begin());
		cout<<t<<"   "<<s<<endl;
		if(t+1<i)
		continue;
		ans=max(ans,i-1);
		if(t==0)
		ans=max(ans,0);
		for(int k=0;k<(int)vec.size();k++)
			cout<<vec[k];
		cout<<endl;
		//cout<<"  "<<t<<endl;
		
		vec.clear();
	}
	
	cout<<ans<<endl;
	
	return 0;
}

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

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n-1)

const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
	int n;
	vi num(100002, 0);

	cin >> n;

	int a, b;

	REP(i, n) {
		cin >> a >> b;

		FOR(j, a, b) {
			num[j]++;
		}
	}

	int ans = 0;
	FOR(i, 1, n) {
		if(i-1 <= num[i]) {
			ans = i-1;
		}
	}

	cout << ans << 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]--;
	}
	num[0]++; num[100002]--;
	REP(i, N+2) num[i+1] += num[i];
	int res = 0;
	REP(i, N+2) if(num[i] >= i) res = max(res, i);
	cout << res-1 << endl;
	return 0;

}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l]++;
        result[r + 1]--;
    }
    int ans = 0;
    for(int i = 2; i < n; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] + 1 >= i) ans = max(i + 1, ans);
    }
    if(ans > 0) ans--;
    cout << ans << endl;
    return 0;
}