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;

int main() {
    int N;
    cin >> N;
    vector<pair<int,int> > P(N);
    for (auto &i : P) cin >> i.first >> i.second;

    int l = 0, r = N + 1;
    while (r - l > 1) {
        int m = (r + l) / 2;
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (P[i].first > m + 1 || P[i].second < m + 1) {
                count++;
            }
        }
        if (m <= N - count) {
            l = m;
        } else {
            r = m;
        }
    }

    cout << l << endl;

    return 0;
}#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<pair<int,int> > P(N);
    for (auto &i : P) cin >> i.first >> i.second;

    int l = 0, r = N + 1;
    while (r - l > 1) {
        int m = (r + l) / 2;
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (P[i].first > m + 1 || P[i].second < m + 1) {
                count++;
            }
        }
        if (m <= N - count) {
            l = m;
        } else {
            r = m;
        }
    }

    cout << l << endl;

    return 0;
}

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

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // use key 100001

int n;
int min_;
int max_;

int solve() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		for (int j = min_; j <= max_; j++) {
			box[j]++;
		}
	}
	for (int i = n + 1; i >= 0; i--) {
		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
int main(){
	int n;
	cin>>n;
	int ans=0;
	int a[MAX],b[MAX];
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=a[j]){
				vec.push_back(b[j]);
			}
		}
		//cout<<i<<endl;
		sort(all(vec));
		int t=0;
		for(int k=0;vec.size()>k&&i<=vec[k];k++)
		t++;
		//int t=(lower_bound(vec.begin(),vec.end(),i)-vec.begin());
		ans=max(ans,t);
		/*for(int k=0;k<(int)vec.size();k++)
		cout<<vec[k];
		cout<<endl;
		cout<<"  "<<t<<endl;
		vec.clear();
		*/
	}
	
	cout<<ans-1<<endl;
	
	return 0;
}

C++: #include<iostream>
using namespace std;
int main() {
	int n; cin >> n;
	int min[114514], max[114514];
	for (int i = 0; i < n; i++) {
		cin >> min[i] >> max[i];
	}
	for (int i = n + 1; i >= 1; i--) {
		int cnt = 0;
		for (int j = 0; j < n; j++) {
			if (min[j] <= i&&i <= max[j])cnt++;
		}
		if (cnt >= i - 1) {
			cout << i - 1 << endl;
			return 0;
		}
	}
	cout << "0\n";
	return 0;
}

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

int imos[100000 + 10];

int main() {

    int n;
    cin >> n;

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

    for(int i=1; i<100000 + 10; i++){
        imos[i] += imos[i-1];
    }

    int ans = 0;

    for(int i=1; i<100000 + 10; i++){
        if(i <= imos[i] + 1){
            ans = i-1;
        }
    }

    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)



int main(void){
	//come[i] := i???(?????????????????????)??§??????????????¨?????????????????????????????????????????°
	int come[100010];

	rep(i, 100010) come[i] = 0;//0??§?????????

	int n; scanf("%d", &n);
	rep(i, n){
		int a, b;
		scanf("%d %d", &a, &b);
		reps(j, a, b + 1){
			come[j]++;
		}
	}

	
	int ans = 0;
	rep(i, 100010){
		//???????????????????????????????????????i?????§????????°??\????????????????????????
		//come[i]????????????????????§???come[i] + 1??????i???????¶????????????????????¢??????????
		if(come[i] + 1 >= i){
			//??????????????????i?????§????????°??\??????????????¨???????????¶?????????????????§???
			//???????????\???????????????????????°???i-1???
			ans = max(ans, i - 1);
		}
	}
	printf("%d\n", --ans);

	return 0;
}

C++: #include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int x[100010];

int main()
{
	int n;
	cin >> n;
	rep(i, n){
		int a, b;
		cin >> a >> b;
		x[a]++; x[b+1]--;
	}
	int cnt = 1, ans = 0;
	rep(i, n+1){
		cnt += x[i];
		if(i <= cnt) ans = max(ans, i-1);
	}
	cout << ans << 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=2; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
  
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]+1);
  }
  cout << mx << endl;
   
  return 0;
}