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>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
random_device rnd;
mt19937 mt(rnd());

const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vector<int> imos(100001 + 10, 0);
    imos[0]++;
    imos[100001 + 1]--;
    rep(i, n)
    {
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b + 1]--;
    }
    int ans = 0;
    int cur = 0;
    rep(i, 100001 + 10)
    {
        cur += imos[i];
        // cerr << i << " " << cur << endl;
        if (i <= cur)
        {
            chmax(ans, i);
        }
    }
    cout << ans - 1 << endl;
}

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

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

int s[100010], n;

int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0);

    cin>>n;
    for(int i=0; i<n; ++i) {
        int a, b; cin>>a>>b;
        ++s[a];
        --s[b+1];
    }

    int res=0;
    for(int i=0; i<=n; ++i) {
        s[i+1]+=s[i];
        if (s[i+1]>=i) res=i;
    }
    cout<<res<<endl;
}

C++: #include <set>
#include <cstdio>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int a[100000 + 3];
int b[100000 + 3];

int main(){
  int n = getInt();

  REP(i,n){
    int aa = getInt();
    int bb = getInt();

    a[aa]++; b[bb]++;
  }

  int ans = 0;
  int peo = 1;

  REP(i,100000 + 3){
    peo += a[i];
    if(i > 0) peo -= b[i - 1];

    if(peo >= i){
      ans = i - 1;
    }
  }

  printf("%d\n", ans);

  return 0;
}

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

using namespace std;

int main(){
	int n;
	cin >> n;
    vector<pair<int , int> > invert(n);

    int nmax=-10,nmin=(int)1e7;
    for(int i = 0; i < n; i++){
        int a , b;
        cin >> a >> b;
        invert[i].first = b;
        invert[i].second = a;

        nmin = min(nmin,a);
        nmax = max(nmax,b);
    }
    sort(invert.begin() , invert.end());

    int ans=0;

    for(int i = nmax; i >= nmin; i--){
        vector<pair<int , int> >::iterator up;
        pair<int , int> mp(i,0);
        up = lower_bound(invert.begin() , invert.end() , mp);
        int lower = distance(invert.begin() , up);
        int c = 0;
        for(int j = lower; j < invert.size(); j++){
            if(invert[j].second <= i){
                c++;
            }
        }
        if(c >= i){
            ans = c;
            break;
        }
    }
    cout << ans-1 << endl;

    return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int N;
	cin >> N;
	std::vector<int> frimin(N);
	std::vector<int> frimax(N);
	for (int i = 0; i < N; i++) {
		cin >> frimin[i] >> frimax[i];
	}

	std::sort(frimin.begin(), frimin.end());
	std::sort(frimax.begin(), frimax.end());

	int ret = 0;
	int count = 1;
	int imin = -1;
	int imax = -1;
	for (int f = 2; f < N+2 && f <= frimax[N-1]; f++) {
		for (int i = imin+1; frimin[i] == f; i++) {
			count++;
			imin++;
		}
		for (int i = imax+1; frimax[i] == f-1; i++) {
			count--;
			imax++;
		}

		if ( count >= f && count != 1 ) {
			ret = f-1;
		}
	}

	cout << ret << endl;
	return 0;
}

C++: #include<stdio.h>
int N,a,b,x[100002],i;
int main()
{
	for(i=!scanf("%d",&N);i<N;++i)
	{
		scanf("%d%d",&a,&b);
		++x[a-1];
		--x[b];
	}
	for(a=b=i=0;i<=N;++i)
		if(b+=x[i],b>=i)
			a=i;
	printf("%d\n",a);
	return 0;
}

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

int main(void){
    int n;
    cin >> n;
    int a[101010] = {};
    int l, r;
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        a[l]++;
        a[r+1]--;
    }
    for (int i = 0; i < n; i++) a[i+1] += a[i];
    
    int ans = 0;
    for (int i = 2; i < n+2; i++) {
        if (a[i]+1 >= i) {
            ans = max(ans, i);
        }
    }
    if (ans) ans--;
    cout << ans << endl;
}

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

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

int main() {

  long long int n;
  cin >> n;

  vector< pair< long long int, long long int > > v;

  for ( long long int i = 0; i < n; i++ ) {

    long long int a, b;
    cin >> a >> b;
    v.push_back( make_pair( 0, a ) );
    v.push_back( make_pair( 1, b ) );

  }

  sort( v.begin(), v.end() );

  long long int ans = 1;
  long long int cnt = 1;
  bool f = false;

  for ( long long int i = 0; i < v.size(); i++ ) {

    if ( v[i].first == 0 ) {
      cnt++;
      if ( cnt >= v[i].second ) f = true;
    }else {
      if ( f == true ) {
	ans = max( ans, min( cnt, v[i].second ) );
      }
      cnt--;
      if ( cnt < v[i].second ) f = false;
    }

  }

  cout << ans - 1 << endl;

  return 0;

}