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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

int n;

bool check(VVI& table, int number) {
    // number ?????????????????????
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (table[i][0] <= number && table[i][1] >= number) {
            count++;
        }
    }
    if (count >= number - 1) {
        return true;
    } else {
        return false;
    }
}

void bs(VVI& table) {
    int left = 0;
    int right = n + 2;

    while (right - left > 1) {
        int middle = (left + right) / 2;
        if (check(table, middle) == false) {
            right = middle;
        } else {
            left = middle;
        }
    }
    cout << left - 1 << endl;
    return;
}

int main(void) {
    cin >> n;
    VVI table(n, VI(2));
    for (int i = 0; i < n; i++) {
        cin >> table[i][0] >> table[i][1];
    }
    bs(table);
    return 0;
}

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

using namespace std;
int N;
int ud[100002];
int main(){
	cin>>N;
	for(int i=0;i<N;i++){
		int a,b;
		cin>>a>>b;
		a--;
		ud[a]++;
		ud[b]--;
	}
	int cnt=0;
	int res=0;
	for(int i=0;i<=N;i++){
		cnt+=ud[i];
		if(cnt>=i)res=i;
	}
	cout<<res<<endl;

	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main()
{
  int N;
  cin >> N;
  static int num[100010];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    ++num[a];
    --num[b+1];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 0; i <= N+1; i++) {
    acc += num[i];
    if (acc >= i-1) {
      ans = i-1;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N, a, b, res;
  signed int invite[100000] = {0};

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> a >> b;
    invite[a-1]++;
    invite[b]--;
    for(int j = 0; j < N+1; j++)
      cout << invite[j] << endl;;
  }

  signed int sum = 0;
  for(int i = 0; i < 100000; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <complex>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cassert>
#include <cstring>
using namespace std;
 
#define REP(i,x,n) for(int i = x; i < (int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define f first
#define s second
#define mp make_pair
#define pb push_back
 
typedef pair<int,int> P;
typedef long long ll;
 
static const int INF = (1 << 29);
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
 
int come[100003];
 
int main(){
  int N;
  scanf("%d",&N);
  rep(i,N){
    int a,b;
    scanf("%d%d",&a,&b);
    come[a]++;
    come[b + 1]--;
  }
  int ans = 0;
  int sum = 1;
  rep(i,100003){
    sum += come[i];
    if(i <= sum) ans = i;
  }
  printf("%d\n",--ans);
  return 0;
}

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

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

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 <stdio.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int N, z[200000];

int main() {
    scanf("%d", &N);
    rep (_, N) {
        int a, b;
        scanf("%d%d", &a, &b);
        z[a]++;
        z[b+1]--;
    }
    int ans = 1, cur = 1;
    rep (i, N+1) {
        cur += z[i];
        if (cur >= i) ans = i;
    }
    printf("%d\n", ans-1);
    return 0;
}

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





int main(){
  int n;
  cin>>n;
  int dp[100010]={};
  
  for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    for(int t=a;t<b+1;t++){dp[t]++;}
  }
  
  int mx=0;
  for(int i=n+1;i>1;i--){
    if(dp[i]>=i-1){mx=i-1; break;}
  }
  cout<<mx<<endl;
  
    
  return 0;
}

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

int imos[100005]={0};

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;++i){
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    imos[0]++;
    int acc = 0, maxi = 0;
    for(int i=0;i<=100001;++i){
        acc += imos[i];
        if(acc >= i)maxi = max(maxi, min(i, acc));
    }

    cout << maxi - 1 << endl;

    return 0;
}