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 a[100002] = {0};
    int temp;
    for(int i=0; i<n; i++){
        cin >> temp;
        a[temp-1]--;
        cin >> temp;
        a[temp]++;
    }

    int ans = 0;
    for(int i=100001; i>=2; i--){
        ans = ans + a[i];
        if(ans+1 >= i){
            cout << i-1 << endl;
            return 0;
        }
    }
    cout << 0 << endl;
  
    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const int mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;


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

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

#define REP(i,a,b) for(int i=int(a);i<int(b);i++)

using namespace std;

typedef long long int lli;

int imos[100010];

int main () {
    int n;
    imos[0]++;
    imos[100002]--;
    cin >> n;
    REP (i, 0, n) {
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b + 1]--;
    }
    REP (i, 1, 100010) imos[i] += imos[i - 1];
    int ans = 0;

    REP (i, 0, 100010) {
        if (imos[i] >= i) ans = i;
    }
    cout << ans - 1 << endl;
    return 0;
}

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

using namespace std;

int main(){
  int num[100005]={0};
  int n,m=0;
  cin>>n;
  int a,b;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    //for(int j=a;j<=b;j++){
    //num[j]++;
    //}
    num[a]++;
    num[b+1]--;
  }

  for(int i=0;i<10004;i++){
    num[i+1]+=num[i];
  }
  for(int i=10004;i>0;i--){
    if(num[i]>=i){
      cout<<i-1<<endl;
      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());
    nmax = min(nmax , n);
    int ans=0;

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

    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
    
int main(){
    int n,a,b,maxs=0;
    cin >> n ;
    vector<int> c(100003,0),sum(100003,0);
    for (int i = 0; i < n ; i++) {
        cin >> a >> b;
        c[a]++;
        c[b+1]--;
    }
    for(int i=0; i<100002 ; i++){
        sum[i+1] = sum[i]+c[i+1];
    }
    for(int i=1;i<=100002;i++){
        if (sum[i]>=i && maxs < i) maxs = i;
    }
    cout << max(0,maxs-1) << endl;
    return 0;
    
    
}

C++: #define REP(i, n) for (int i = 0; i < (n); ++i)
#include <cstdio>
#include <cstring>
int data[100002];

int main() {
  int n;
  scanf("%d", &n);

  memset(data, 0, sizeof(data));

  int b_max = 0;
  REP(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    data[a]++;
    data[b + 1]--;

    if (b > b_max) b_max = b;
  }

  REP(i, b_max + 1) data[i] += data[i - 1];

  int ans = 0;
  REP(i, b_max + 1) {
    if (data[i + 1] >= i) ans = i;
  }

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

C++: #include <cstdio>

using namespace std;

int main(void)
{
	int N;
	int a[100000], b[100000];
	
	scanf("%d", &N);
	
	for (int i = 0; i < N; i++){
		scanf("%d %d", &a[i], &b[i]);
		a[i]--; b[i]--;
	}
	
	int left = 0, right = N;
	
	while (left != right){
		int center = (left + right + 1) / 2;
		
		int ans = 0;
		for (int i = 0; i < N; i++){
			if (a[i] <= center && center <= b[i]){
				ans++;
			}
		}
		
		if (ans >= center){
			left = center;
		}
		else {
			right = center - 1;
		}
	}
	
	printf("%d\n", left);
	
	return (0);
}

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

using namespace std;

int main(void){
  int n;
  cin>>n;
  int a,b;
  int dp[100010] = {0};
  for(int i = 0 ; i < n ; i ++){
    cin>>a>>b;
    dp[min(a,n+1)]++;
    dp[min(b+1,n+1)]--;
  }
  int ret = 0;
  for(int i = 0 ; i < 100009 ; i ++){
    dp[i+1] += dp[i];
    if(i-1<=dp[i])ret = max(ret,i-1);
  } 
  cout<<ret<<endl;
}

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

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

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

	map<ll, int> mp;	

	rep(i, n){
		ll a, b;
		cin >> a >> b;
		mp[a]++;
		mp[b+1]--;
	} 
	
	ll num = 1;
	ll ans = 1;
	for(auto it = mp.begin(); it != mp.end(); it++){
		//cout << it->second << endl;
		num += it->second;
		if(num >= it->first) ans = max(ans, it->first);
	}
	//cout << endl;
	cout << ans - 1 << endl;
	

	return 0;
}