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);
  ans[0] = 0;
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    --a;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,100001){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,0,n){
    k = max(k,ans[i]); 
  }
  if(n > 1){
    if(k <= 1) cout << 0 << endl;
    else
      cout << k-1 << endl;
  }
  else{
    cout << k << endl;
  }
  return 0;
}

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

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

    int r[100003]={0};
    rep(i,n)
    {
        int a,b;
        scanf(" %d %d", &a, &b);
        ++r[a];
        --r[b+1];
    }

    rep(i,100001) r[i+1]+=r[i];

    int ans=n;
    while(ans>0 && r[ans+1]!=n) --ans;

    printf("%d\n", ans);
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

P a[1000000];
int n;

int search(int m){
  int c=1;
  rep(i,n){
    if(a[i].first<=m&&m<=a[i].second)
      c++;
  }

  if(c>=m) return true;

  return false;
}


int main(){
  cin>>n;
  rep(i,n){
    int b,c;
    cin>>b>>c;
    a[i]=P(b,c);
  }

  int r=0,l=inf;

  while(l-r!=1){
    int m = (r+l)/2;
    if(search(m))
      r = m;
    else
      l = m;
  }

  cout<<r-1<<endl;

}

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

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int Digit[100002];
	for(int i = 0; i < 100002; ++i)
	{
		Digit[i] = 0;
	}
	int N;
	cin >> N;
	vector<P> Person(N);
	for(int i = 0; i < N; ++i)
	{
		cin >> Person[i].first >> Person[i].second;
	}
	for(int i = 0; i < N; ++i)
	{
		for(int j = Person[i].first; j <= Person[i].second; ++j)
		{
			++Digit[j];
		}
	}
	int Max = 1;
	for(int i = 2; i <= N; ++i)
	{
		if(Max < Digit[i])
		{
			Max = Digit[i];
		}
	}
	cout << Max - 1 << endl;
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio> 
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)

int canGo[100001] = {0};

class Solve {
public:
	Solve() {};
	~Solve() {};

	void solver() {
		int ans = 0;
		int n;
		std::cin >> n;
		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));
		rep(i, n) {
			std::cin >> friends[i].first >> friends[i].second;
			//Exclude myself
			friends[i].first--; friends[i].second--;
		}
		//Record the number of people who can go.
		for (auto& x : friends) {
			canGo[x.first]++;
			canGo[x.second + 1]--;
		}
		//fill
		rangeRep(i, 1, n + 1) {
			canGo[i] += canGo[i - 1];
		}
		int max = 0;
		rep(i, n+1) {
			if (canGo[i] >= i) {
				max = i;
			}
		}
		std::cout << max << std::endl;
	}
private:

};

int main() {
	Solve solve;
	solve.solver();
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
    
int main(){
    int n,a,b,maxs=0;
    cin >> n ;
    vector<int> c(100003,0),sum(100003,0);
    for (int i = 0; i < n ; i++) {
        cin >> a >> b;
        c[a]++;
        c[b+1]--;
    }
    sum[0]=1;
    for(int i=0; i<100002 ; i++){
        sum[i+1] = sum[i]+c[i+1];
    }
    for(int i=1;i<=100002;i++){
        if (sum[i]>=i) maxs = i;
    }
    cout << max(0,maxs-1) << endl;
    return 0;
    
    
}

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

signed main(){
	int N;
	cin >> N;
	vector<int> dp(100000+5, 0);
	dp[0] = 1;
	for(int i=0;i<N;i++)
	{
		int a,b;
		cin >> a >> b;
		dp[a]++;
		dp[b+1]--;
	}
	for(int i=1;i<=100001;i++){
		dp[i] += dp[i-1];
	}
	for(int i=100001;i>=0;i--){
		if(dp[i]>=i){
			cout << i-1 << endl;
			break;
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int a[100001],b[100001];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	sort(a,a+n);
	sort(b,b+n);
	b[n]=-1;
	int ans=1,top=0;
	int k=1;
	for(int i=0;i<n;i++){
		k++;
		while(b[top]+1==k)
			top++,k--;
		if(a[i]<=k)
			ans=max(ans,k);
	}
	cout<<ans-1<<endl;
	return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

static int member[100002];

void solve()
{
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    member[b]++;
    member[a - 1]--;
  }
  for(int i = 1e6 + 1; i > 0; i--) {
    if (i != 1e6 + 1) member[i] += member[i + 1];
    if(member[i] + 1 >= i) {
      cout << i - 1 << "\n";
      return;
    }
  }
}

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

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

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