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>
using namespace std;

int main()
{
  int N;
  cin >> N;
  int c[N+3] = {};
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    c[a]++;
    c[b+1]--;
  }
  int ans = 0;
  int sum = 1;
  for(int i = 0; i < N+2; i++){
    sum += c[i];
    ans = (i <= sum) ?  i : ans;
  }
  cout << ans - 1 << endl;
}

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

#define P pair<int, int>
using namespace std;

int N;
pair<int, int> ab[100005];
int end[100005] = {0};

priority_queue< P, vector< P >, greater< P > > Q;

int main(void)
{
	cin >> N;
	for(int i = 1; i <= N; i++){
		cin >> ab[i].first >> ab[i].second;
	}
	
	for(int i = 1; i <= N; i++){
		Q.push( ab[i] );
	}
	
	int num = 0;
	int max = 0;
	
	
	for(int i = 1; i <= 100005; i++){
		
		
		
		num -= end[i];
		while(Q.top().first == i && (int)Q.size()){
			num++;
			end[Q.top().second + 1]++;
			Q.pop();
		}
		
		if(num >= i-1) max = i;
		if((int)Q.size() == 0) break;
		
	}
	
	end:
	cout << max-1 << endl;
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <numeric>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<((x))<<endl
#define fi first
#define se second

#define INF 2147483600
#define long long long

int a[100005], b[100005];

int main(){
  int n;
  cin>>n;
  rep(i,n) scanf("%d %d", a+i, b+i);
  vector<int> vec(100005,0);
  rep(i,n){
    vec[a[i]]++;
    vec[b[i]+1]--;
  }
  rep(i,n+1) vec[i+1] += vec[i];

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

  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main()
{
    int N;
    cin >> N;
    constexpr int MAX = 100000;
    vector<int> num(MAX + 1, 0);
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        num[a]++;
        if (b + 1 <= MAX) {
            num[b + 1]--;
        }
    }
    for (int i = 1; i <= MAX; i++) {
        num[i] += num[i - 1];
    }

    for (int i = MAX; i >= 0; i--) {
        if (num[i] >= i) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

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

int n;
int pls[100002], mns[100002];

int main(){
  while(scanf("%d", &n) != EOF){
    memset(pls, 0, sizeof(pls));
    memset(mns, 0, sizeof(mns));

    for(int i = 0; i < n; i++){
      int a, b;
      scanf("%d%d", &a, &b);
      pls[a]++;
      mns[b]++;
    }

    int sum = 0;
    int ans = 1;

    for(int i = 1; i <= n + 1; i++){
      sum += pls[i];

      if(i <= sum + 1){
        ans = i;
      }

      sum -= mns[i];
    }

    cout << ans - 1 << endl;
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100011] = {0}, ans = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--;
            count[a]++;
            count[b]--;
        }
        int sum = 0;
        rep(i,n + 1){
            sum+=count[i];
            if(sum >= i && ans < i) ans = i;
        }
        cout << ans << endl;
    }
}

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

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

C++: 
#include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
#include <math.h>

using namespace std;

typedef long long int ll;

const int M = 100001+3;
ll solve( ll N );


int main(void) {
	
	ll N;
	cin >> N;
	cout << solve(N) << endl;
	
	return 0;
}


ll solve( const ll N ) {

	int ans = 0;

	//?´????????????¨???
	vector< int > v(M,0);

	v[0]++;
	v[v.size()-1]--;
	for( ll i = 0; i < N; i++ ) {
		int a, b;
		cin >> a >> b;
		v[a]++;
		v[b+1]--;
	}

	for( ll i = 0; i+1 < M; i++ )
		v[i+1] += v[i];

	for( int i = 0; i < M; i++)
		if( min(v[i],i) > ans && (v[i] >= i) )
			ans = min(v[i],i);

	return max(ans-1,0);

}

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

int ruisekiwa[100005];
int main()
{
	int a; cin >> a;
	for (int b = 0; b < a; b++) {
		int c, d; scanf("%d%d", &c, &d);
		ruisekiwa[c]++; ruisekiwa[d + 1]--;
	}
	int k = 1;
	int MAX = 0;
	for (int i = a+1; i >=0; i++) {
		k += ruisekiwa[i];
		if (k >= i) { cout << i - 1 << endl; break; }
	}
}

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

int data[100005];

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

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[j]++;
	}

	int ans = 0;
	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = data[i];
		if(ok + 1 >= i) ans = max(ans, i);
	}

	cout << ans - 1 << endl;

	return 0;
}

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

struct State{
	int x, y; 
	State(int x_, int y_){ x = x_; y = y_; }
	State(){}
};
bool operator<(const State& a, const State& b){
	if( a.x > b.x ) return true;
	else if( a.x < b.x ) return false;
	return a.y > b.y;
}

int main(){
	int n;
	priority_queue<State> q;
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		int a, b;
		cin >> a >> b;
		q.push( State(a, +1) );
		q.push( State(b, -1) );
	}
	
	q.push( State(n+10, +1) );
	int ans = 0, num = 0, f = 1;
	while( !q.empty() ){
		State s = q.top(); q.pop();
		while( f != s.x ){
			if( f-1 <= num ) ans = f-1;
			f++;
		}
		num += s.y;
	}
	cout << ans << endl;
}

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