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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

double dist(Point p) { return abs(p); }//ベクトルpの絶対値
double dist(Point a, Point b) {//2点間の距離
	return abs(a - b);
}

int n;
int sum[100100];

int main() {
	cin >> n;
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	rep(i, 100100 - 1)sum[i + 1] += sum[i];
	ll ans = 0;
	rep(i, n + 1) {
		ll num = i + 1;
		if (sum[num] > i)ans = i;
	}
	cout << ans << 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 (fri <= friends[i] && friends[i] >= i) {
        fri = friends[i];
        maxi = i;
    }
  }

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

  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int p[111111];
int main(void){
  cin >> n;
  for(int i = 0; i < n; i++){
    int a,b;
    cin >> a >> b;
    p[a-1]++;
    p[b]--;
  }
  int best=0;
  for(int i = 1; i <= 100001; i++){
    p[i] += p[i-1];
    if(i < p[i]){
      best = i;
    }
  }
  cout << best << endl;
}

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
 
typedef long long ll;
typedef pair<int, int> ii;

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

	map<ll, int> mp;	

	rep(i, n){
		ll a, b;
		cin >> a >> b;
		mp[a]++;
		mp[b];
		mp[b+1]--;
	} 
	
	ll num = 1;
	ll ans = 1;
	for(auto it = mp.begin(); it != mp.end(); it++){
		//cout << it->second << endl;
		num += it->second;
		if(num >= it->first) ans = max(ans, min(it->first, num));
	}
	//cout << endl;
	cout << ans - 1 << endl;
	

	return 0;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  int n; cin >> n;

  vi table(100003, 0);
  REP(i, n) {
    int l, r;cin >> l >> r;
    table[l]++;
    table[r+1]--;
  }
  int ma = 0;
  REP(i, n+2) {
    table[i] += table[i-1];
    if(table[i]+1 >= i) {
      ma = i;
    }
  }

  cout << min(n, max(ma-1, 0)) << endl;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
  for(;;) {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<pair<int,int>> abs;
    for(int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      abs.push_back(make_pair(a, b));
    }
    sort(abs.begin(), abs.end());
    int s = 0;
    int best = 0;
    for(int k = 1; k <= n; k++) {
      while(s < n && abs[s].first <= k+1) s++;
      int c = k;
      for(int j = s - 1; c > 0 && j >= 0; j--) {
        if(abs[j].second >= k+1) c--;
      }
      if(c == 0) { best = k; }
    }
    cout << best << endl;
    break;
  }
  return 0;
}

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

int data[100005];

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

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[j]++;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = data[i];
		if(ok + 1 >= i) v.push_back(i);
	}

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<utility>
using namespace std;
int main(void){
    int n;
    cin>>n;
vector<int> min(n),max(n);
for(int i=0;i<n;i++)
cin>>min[i]>>max[i];
for(int i=n+1;i>0;i--){
    int l=0;
    for(int j=0;j<n;j++){
        if(min[j]<=i&&i<=max[j])
        l++;
    }
    if(l+1>=i){
cout<<i-1<<endl;
        break;
    }
}
return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n;cin>>n;
    vector<int> k(100002);
    int ma=0;
    for(int i=0;i<n;i++){
        int a,b;cin>>a>>b;
        ma=max(ma,b);
        k[a-1]--;
        k[b]++;
    }
    int count=1;
    for(int i=ma;i>=0;i--){
        count+=k[i];
        if(count>=i){
            count=i;break;
        }
    }
    cout<<count-1<<endl;
}

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;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--;
            count[a]+= 1;
            count[b]-= 1;
        }
        rep(i,n + 1) count[i + 1]+=count[i];
        rep(i,n + 1){
            if(count[i] >= i) ans = i;
        }
        cout << ans << endl;
    }
}