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 n,a,b;
int member[100004];
int sum;;

int main(){
	member[1]=1;
	cin >>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		member[a]++;
		member[b+1]--;
	}
	sum=0;
	for(int i=1;i<n;i++){
		member[i+1]+=member[i];
		if(i+1<=member[i+1])sum=i;
	}
	cout<<sum <<endl;
}

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

using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> num(100001,0);
	for(int i=0;i<n;i++){
		int t,s;
		cin >> t >> s;
		t--;
		s--;
		num[t]++;
		num[s]--;
	}
	int ans=0,temp=0,maxi=0;
	for(int i=1;i<100001;i++){
		temp+=num[i];
		if(temp>ans && i<=temp){
			ans=temp;
		}
	}
	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int main() {

    int n;
    cin >> n;
    vector< vector<int> > p(n, vector<int>(100001));

    int ma=0;
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        ma = max(ma, b);
        for (int j=a; j<=b; ++j) {
            p[i][j]++;
        }
    }

    int ans = 0;
    rep(i, ma) {
        int t = 0;
        rep(j, n) {
            t += p[j][i];
        }
        if (i <= t) {
            ans = i;
        }
    }

    cout << ans << endl;

}

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


int e[100002];

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		e[a]++;
		e[b+1]--;
	}
	int ans=0,c=0;
	for(int i=1;i<=100000;i++){
		c+=e[i];
		if(i<=c+1)ans=i-1;
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<math.h>
#include <string>
#define cinf(n,x,y) for(int i=0;i<(n);i++) cin >> x[i]>>y[i];
typedef long long int ll;
using namespace std;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }

int cnt[100010];
int main(){
	int n; cin >>n;
	vector<int> a(n),b(n);
	cinf(n,a,b);
	for(int i=0;i<n;i++){
		cnt[a[i]]++;
		cnt[b[i]+1]--;
	}
	for(int i=0;i<100010;i++){
		cnt[i+1]+=cnt[i];
	}
	int ans=0;
	for(int i=0;i<100010;i++){
		if(i<=cnt[i]+1){
			ans=max(ans,i);
		}
	}
	cout << max(ans-1,0) << endl;
}

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)
		{
			if(maxn<sum)
				maxn = sum;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;char c;char str[1024];string s;ll l;
	int nextInt(){scanf("%d", &n);return n;}
	ll nextLong(){scanf("%lld", &l);return l;}
	char nextChar(){scanf("%c", &c);return c;}
	string next(){scanf("%s", str);return string(str);}
	string nextLine(){getline(cin, s);return s;}
};
Input in;

int c[100100];

int main(){
	int n = in.nextInt();
	vi a(n), b(n);
	rep(i, n) a[i] = in.nextInt(), b[i] = in.nextInt();

	vector<pii> p;
	rep(i, n) p.pb({a[i], b[i]});
	sort(all(p));

	int ans = 0;

	int idx1 = 0, idx2 = 0;
	for(int i = 0; i < n + 1; i++){
		//i + 1 nin de ikeru tomodachi no kazu
		int idx = upper_bound(all(p), mp(i + 1, INF)) - p.begin();
		//c[x] := x nin ijou ha dame
		for(int j = idx1; j < idx; j++){
			if(p[j].se < i) c[i]++;
			else c[p[j].se]++;
		}
		idx1 = idx;
		if(i > 0) c[i] += c[i-1];
		// i + 1 nin atumaranai
		if(i > idx || i > idx - c[i]) continue;
		//tomodachi ha i nin
		ans = max(ans, i);
	}

	cout<<ans<<endl;
}

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;
			return 0;
		}
	}
	cout << 0;
	return 0;
}