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 <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <string.h>
#include <numeric>
using namespace std;

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) rep(i,(n)+1)
#define repst(i,s,n) reps(i,s,(n)+1)
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (((i)==(n)-1)?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln<<flush
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ')<<flush;}}
#define zero(a) memset(a,0,sizeof(a))
#define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end())

//typedef complex<double> P;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

ll mop(ll a,ll b,ll m=mod) {ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a*b/gcd(a,b);}
bool ool(int x,int y,int h,int w) {return((x<0)||(h<=x)||(y<0)||(w<=y));}
bool deq(double a,double b) {return abs(a-b)<eps;}

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;



int n, a, b, s[114514], res;

signed main() {
	cin >> n;
	rep(i,n) {
		cin >> a >> b;
		s[a]++, s[b+1]--;
	}
	rep(i,100100) {
		s[i+1] += s[i];
		if (s[i]>=i-1) chmax(res, i-1);
	}
	cout << res << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)

int T[100003] = {};

int main()
{
	int N;
	cin >> N;
	REP(i, N)
	{
		int t1, t2;
		cin >> t1 >> t2;
		T[t1]++;
		T[t2+1]--;
	}
	int ans = 0;
	int temp = 1;
	REP(i, 100003)
	{
		temp += T[i];
		if(i<=temp)ans = i;
	}
	cout << ans-1 << endl;
	return 0;
}

C++: #include <cstdio>
int h[100010];

typedef unsigned int uint;
int in() {
    int x = 0, c;
    for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; }
    do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10);
    return x;
}

int main(){
	int n = in();
	for(int i = 0 ; i < n ; i++){ int a = in() , b = in() ; h[a-1]++ , h[b]--; }
	int ans = 0 , cur = 0;
	for(int i = 0 ; i <= 100001 ; i++){
		cur += h[i];
		if(cur >= i) ans = i;
	}
	printf("%d\n",ans);
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
int main(){
	int n;
	cin>>n;
	int ans=0;
	int a[MAX],b[MAX];
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=a[j]){
				vec.push_back(b[j]);
			}
		}
		//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 t=(lower_bound(vec.begin(),vec.end(),i)-vec.begin());
		//cout<<i<<"   "<<t<<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 <iostream>
using namespace std;

int main()
{   
    int n, m;
    int a, b;
    int i, j, k, l;
    int h_now = 0, w_now = 0;
    int h[1505], w[1505];
    while(1){
        w_now = 0;
        h_now = 0;
        h[0] = 0;
        w[0] = 0;
        cin >> n >> m;
        if(n == 0 && m == 0){ break ;}
        
        //????????§?¨?
        for(i = 1; i <= n; i++){
            cin >> a;
        
        h_now = h_now + a;
        h[i] = h_now;
        }

        //w?????§?¨?
        for(i = 1; i <= m; i++){
            cin >> b;

            w_now = w_now + b;
            w[i] = w_now;
        }
        int length_h, length_w;
        int counter = 0;
        for(i = 0; i < n; i++){
            for(j = i + 1; j <= n; j++){
                length_h = h[j] - h[i];

                for(k = 0; k < m; k++){
                    for(l = k; l <= m; l++){
                        length_w = w[l] - w[k];
                        if(length_h == length_w){ counter++ ;}
                    }
                }
            }
        }
        cout << counter << endl;

    }
    return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

	cin >> N;
	vector<int>sum(100003);
	for (int i = 0; i < N; i++) {
		cin >> L >> R;
		sum[L]++;
		sum[R + 1]--;
	}
	sum[0]++;
	for (int i = 1; i <= 100001; i++) {
		sum[i] += sum[i - 1]; 
	}
	for (int i = 100001; i >= 0; i--) {
		if (i <= sum[i]) {
			cout << i - 1 << endl;
			return 0;
		}
	}
	return 0;
}