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 <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int field[100001];
  int N;
  cin >> N;
  memset(field, 0, sizeof(field));
  int a, b;
  while(N--){
    cin >> a >> b;
    field[a]++;
    field[b]--;
  }

  int ans = 0, sum = 0;

  for(int i = 1 ; i < 100001 ; i++){
    sum += field[i];
    if(i <= sum+1) ans = i;
  }
  
  cout << ans-1 << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
 
typedef long long ll;
typedef pair<int, int> ii;

int main() {
	
	ll n;
	cin >> n;
	map<int, int> mp;
	rep(i, n){
		ll a, b;
		cin >> a >> b;
		mp[a]++;
		mp[b+1]--;
	}
	
	int ans = 0;
	int tmp = 0;
	for(auto it : mp){
		tmp += it.second;
		if(tmp >= it.first-1) ans = max(ans, it.first-1);
	}
	
	cout << ans << endl;
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef pair<int,int> P;

int n;
int x[200000];

int main(){
	cin >> n;
	rep(i,n){
		int a, b; cin >> a >> b;
		x[a]++;
		x[b+1]--;
	}
	for(int i = 1; i < n+100; i++) x[i] += x[i-1];
	int ans = 1;
	for(int i = 1; i <= n+1; i++){
		if(x[i] == n) ans = i;
	}
	cout << ans-1 << endl;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
 
#define rep(i,n) for (int i = 0; i < (n); ++i)
  
int n, a, b, friends[100005], fri, maxi;

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

  std::cin >> n;
  rep(i,n) {
    std::cin >> a >> b;
    a--; b--;
    for (int i = a; i <= b; ++i) friends[i]++;
  }
  rep(i,n+1) {
    if (friends[i] >= i) {
        maxi = i;
    }
  }

  std::cout << maxi << std::endl;

  return 0;
}

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

using namespace std;

int N, a[100010], b[100010];

bool check(int m) {
    int cnt = 0;
    for (int i = 0; i < N; i++)
        if (a[i] <= m && m <= b[i]) cnt++;
    
    if (cnt + 1 < m) return false;
    else return true;
}

int main(void) {
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> a[i] >> b[i];
    
    int l = 1, r = N + 2;
    while (r - l > 1) {
        int m = (l + r) / 2;
        if (check(m)) l = m;
        else r = m;
    }
    cout << l - 1 << endl;

    return 0;
}

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

using namespace std;
	

int main(){
	int N;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;
	}
	/*
	for(int range:nokemono){
		cout << range << endl;
	}
	*/
	
	bool flag = true;
	for(int i=N+1;i>0;i--){
		if(humans[i] < i) continue;
		if(nokemono[i] < humans[i]) continue;
		cout << i << endl;
		flag = false;
		break;
	}
	
	if(!false)
		cout << 0 << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100001] = {0}, ans = 0, sum = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--;
            count[a]+= 1;
            count[b]-= 1;
        }
        rep(i,n + 1){
            sum+=count[i];
            if(sum >= i) ans = i;
        }
        cout << ans << endl;
    }
}

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 min_;
int max_;

int solve() {
	cin >> n;
	for (int i = 0; i <= n + 1; i++) {
		box[i] = 0;
	}
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		for (int j = min_; j < max_; j++) {
			box[j]++;
		}
	}
	for (int i = n + 1; i >= 0; i--) {
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	cout << solve() << endl;

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

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

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n;
	cin >> n;
	vi mi(n);
	vi ma(n);
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		a--;
		mi[i]=a;
		ma[i] = b;
	}
	SORT(mi);
	SORT(ma);
	int cnt = 0;
	int iidx = 0;
	int aidx = 0;
	int ans = 0;
	REP(i, 100001) {
		while (iidx<n&&mi[iidx] == i) {
			cnt++;
			iidx++;
		}
		while (aidx<n&&ma[aidx] == i) {
			cnt--;
			aidx++;
		}
		if (i <= cnt)
			ans = i;
	}
	cout << ans << endl;
}

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



int main() {
	int N;
	int num[100002];

	cin >> N;


	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		for (int j = a; j <= b && j <= N; j++) {
			++num[j];
		}
	}

	
	for (int i = N + 1; i > 0; i--) {
		if (num[i] >= i - 1) {
			cout << i - 1 << endl;
			break;
		}
	}



	return 0;
}