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;

static int member[101010];

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

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int MOD=1e9+7;
const int INF=1e9;
const double ESP=1e-5;
template<typename T> void cmax(T &a, T b) {a = max(a, b);}
template<typename T> void cmin(T &a, T b) {a = min(a, b);}

int d[100005];
signed main(){
  int n;
  cin>>n;
  rep(i,n){
    int a,b;
    cin>>a>>b;
    a--;
    d[a]++;
    d[b]--;
  }
  int ans=0;
  rep(i,100000)d[i+1]+=d[i];
  loop(i,1,100001)if(d[i]>=i)ans=i;
  cout<<ans<<endl;
 return 0;
}

C++: #include <iostream>
#include <algorithm>
#define N 100000
int a[N],b[N],c[N];
using namespace std;
int main( void )
{
  int i,j,k,n,d=0,ret=0;
  cin >> n;
  for( i=0; i<n; i++ )
    cin >> a[i] >> b[i];
  sort( a, a+n );
  sort( b, b+n );
  for( i=2,j=0,k=0; i<=n+1; i++ ){
    while( a[j] == i ){ j++; d++; }
    while( b[k] == i-1 ){ k++; d--; }
    if( i <= d ) ret = i-1;
  }
  cout << ret << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
    int N; cin >> N;
    vector<int> num(N + 3);
    rep(i, N) {
        int a, b; cin >> a >> b;
        if (a > N + 1) continue;
        num[a] += 1; num[min(b, N + 1) + 1] -= 1;
    }
    int maxi = 0;
    for (int i = 2; i < N + 3; ++i) {
        num[i] += num[i - 1];
        if (num[i] >= i - 1 && maxi < i - 1) maxi = i - 1;
    }
    cout << maxi << endl;
}

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

using namespace std;

pair<int,int> friends[100000];
int main(){
  int N; 
  cin >> N;
  for(int i=0;i<N;++i){
    cin >> friends[i].first >> friends[i].second;
  }
  
  int max_num = 1;
  for(int i=2;i<=100001;++i){
    int num = 1;
    for(int j=0;j<N;++j){
      if(friends[j].first<=i&&i<=friends[j].second){
        num++ ;
      }
    }
    if(num >=i) max_num = i;
  }
  cout << max_num-1 << endl;
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cassert>
using namespace std;
int main(){
  int n,st,ed;
  int info[100100][2],total,lim; // 0 -> st,1 -> ed
  while(cin >> n){
    total = 0; lim = 1;
    for(int i=0;i<100100;i++)info[i][0] = info[i][1] = 0;
    info[0][0] = info[1][0] = 1;
    for(int i=0;i<n;i++){
      cin >> st >> ed; lim = max(lim,ed);
      info[st][0]++;
      info[ed][1]++;
    }
    int ans = 0;
    for(int i=1;i<=lim;i++){
      if(info[i][0] != 0 && total+info[i][0] >= i)total+=info[i][0];
      if(info[i][1] != 0 && total > i){
	total -= (total-i); 
      }
     
      assert(total>=0);
      ans = max(ans,total);
    }

    cout << ans-1 << endl;

  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include<vector>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
using namespace std;

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

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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  
  int n;
  cin >> n;

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

  int ans = 1;
  for (int i = 1; i < 100010; i++) {
    if (cum[i] >= i - 1) ans = i;
  }
  cout << ans - 1 << endl;

  return 0;
}

C++:  #include <iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define P pair<int,int>
#define mp make_pair


int t[100002];
int x[100002];
int y[100002];
int z[100002];
int n;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
			for(int j=a-1;j<b;j++)t[j]++;
			y[a]++;
			z[b]--;
	}
	
	int ans=0;
	//sfor(int i=0;i<=n;i++)cout<<t[i]<<" ";cout<<endl;
	for(int i=0;i<=n+1;i++)if(t[i]==i+1)ans=i;//t[i]??\??????????????°i???????????°


	cout<<ans<<endl;
	return 0;

}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
 
//typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INF=INT_MAX;
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const int MAX_SIZE=400007;
LL segMax[MAX_SIZE - 1], segAdd[MAX_SIZE - 1];
int n;

//??????[a, b)??????x???????????????.
void add(int a, int b, int x, int k = 0, int l = 0, int r = n)
{
	if (r <= a || b <= l) return;
	
	if (a <= l && r <= b){
		segAdd[k] += x;
		while (k){
			k = (k - 1) / 2;
			segMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1], segMax[k * 2 + 2] + segAdd[k * 2 + 2]);
		}
		return;
	}
	
	add(a, b, x, k * 2 + 1, l, (l + r) / 2); 
	add(a, b, x, k * 2 + 2, (l + r) / 2, r); 
}

LL getMax(int a, int b, int k = 0, int l = 0, int r = n)
{
	if (r <= a || b <= l) return 0LL;
	
	if (a <= l && r <= b) return (segMax[k] + segAdd[k]); 
	
	LL left = getMax(a, b, k * 2 + 1, l, (l + r) / 2);
	LL right = getMax(a, b, k * 2 + 2, (l + r) / 2, r); 
	
	return (max(left, right) + segAdd[k]);
	
}
int main(){
	CLR(segMax);CLR(segAdd);
	int N;
	cin>>N;
	n=100003;
	REP(i,N){
		int l,r;
		cin>>l>>r;
		add(l,r+1,1);
	}
	int res=0;
	REP(i,100001){
		if(getMax(i,i+1)+1>=i){
			res=i;
		}
		//if(i<=10)cout<<i<<" "<<getMax(i,i+1)<<endl;
	}
	
	cout<<res-1<<endl;
	return 0;
}