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+1){
    if(ans[i] >= i)
      k = max(k,i+ 0LL); 
  }
  cout << k << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=1<<30;
const int mod=1e9+7;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    vector<int> a(n),b(n),c(100005);
    for(int i=0;i<n;i++){
        cin >> a[i] >> b[i];
        c[a[i]-1]++,c[b[i]]--;
    }
    int sum=0,ans=0;
    for(int i=1;i<=n;i++){
        sum+=c[i];
        if(sum>=i){
            chmax(ans,i);
        }
    }
    cout << ans << endl;
}

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 f[100005];

int main() {

    int n;
    int a, b;
    cin >> n;

    rep(i, n) {
        cin >> a >> b;
        f[a]++;
        f[b+1]--;
    }

    int sum=1;
    int ans=0;
    rep(i, 100005) {
        sum += f[i];
        if (i <= sum) {
            ans=i;
        }
    }

    cout << ans-1 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+2){
    if(a[i]>=i) ans=i;
  }
  if(ans==0) cout<<0<<endl;
  else 
  cout<<ans-1<<endl;
  return 0;
    
}

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

int memo[100006] = {0};
int n, ans;

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

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

int main(){
    int n;  cin>>n;
    int OK[n][2];   for(int i=0;i<n;i++) cin>>OK[i][0]>>OK[i][1];
    
    int result[n+2];  for(int i=0;i<n+2;i++)result[i]=0;

    for(int i=1;i<=n+1;i++){ // i は（私を含め）行く人数
        for(int j=0;j<n;j++){
            if(OK[j][0]<=i && i<=OK[j][1])result[i]++;
        }
    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

    for(int i=0;i<n+2;i++){
        if(i<=result[i])result[i]=i-1;
        else if(i-1>result[i])result[i]=-1;
    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

    int max=0;
    for(int i=0;i<n+2;i++){
        if(result[i]>max)max=result[i];
    }

    cout<<max<<endl;


    return 0;
}

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

using namespace std;

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

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int segtree[262144];
void add(int a,int b,int c,int d,int e){
	if(d<a||b<c)return;
	if(c<=a&&b<=d){segtree[e]++;return;}
	add(a,(a+b)/2,c,d,e*2);
	add((a+b)/2+1,b,c,d,e*2+1);
}
int get(int a){
	int ret=0;
	a+=131072;
	while(a){
		ret+=segtree[a];
		a/=2;
	}
	return ret;
}
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a;i++){
		int p,q;scanf("%d%d",&p,&q);
		p--;q--;
		add(0,131071,p,q,1);
	}
	int ret=0;
	for(int i=0;i<110000;i++){
		if(get(i)>=i)ret=i;
	}
	printf("%d\n",ret);
}

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

int main()
{
	int N,x[100005]={0};
	cin >> N;
	for(int i=0; i<N; i++)
	{
		int a,b;
		cin>>a>>b;
		x[a]++; x[b+1]--;
	}

	int ans=0,tmp=0;
	for(int i=2; i<N+2; i++)
	{
		tmp+=x[i];
		if(tmp>=i-1)
		ans=max(ans,min(tmp,i-1));
	}

	cout << ans << endl;
}

C++: #include<iostream>

using namespace std;

int main(){

  int n, a, b;
  int c[100010]={};

  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++) c[j]++;
  }

  int ans=0;
  for(int j=2;j<=100001;j++) if(j-1<=c[j]) ans=j-1;

  cout<<ans<<endl;
  
  return 0;
}

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

int main()
{
  int N;
  cin >> N;
  static int v[100010];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    ++v[a];
    --v[b+1];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 0; i <= N+1; i++) {
    acc += v[i];
    if (acc >= i-1) {
      ans = i-1;
    }
  }
  cout << ans << endl;
  return 0;
}