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;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int main(void) {
  int64 n;
  int64 res = 0;
  vector<PLL> q;
  vector<int64> sum(100010, 0);
  cin >> n;
  REP(i, n) {
    int64 a, b;
    cin >> a >> b;
    sum[a] += 1;
    sum[b+1]--;
  }
  REP(i, sum.size()-1) sum[i+1] += sum[i];
  REP(i, sum.size()) {
    if (sum[i] >= i-1) {
      res = max(res, i-1);
    }
  }
  cout << res << endl;
}

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 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]++;
	}
	
	int ans=0;
	//for(int i=0;i<=n;i++)cout<<i<<" "<<t[i]<<endl;
	for(int i=1;i<=n+1;i++)y[i]+=y[i-1];
	//for(int i=0;i<=n;i++)cout<<i<<" "<<y[i]<<endl;

	for(int i=1;i<=n+1;i++)if(i>=t[i]&&y[i]>=i)ans=max(ans,t[i]);
	for(int i=0;i<=n;i++)t[i]=y[i]=0;
	cout<<ans<<endl;
	return 0;

}

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

#define PI 4*atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

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

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>

int change[100010] = {};
int main() {
	int N = 0;
	scanf("%d",&N);
	for (int i = 0; i < N;i++) {
		int a, b;
		scanf("%d %d",&a,&b);
		change[a]++;
		change[b+1]--;
	}
	int ans=0;
	int p=0;
	for (int i = 2; i <= N + 1; i++) {
		p += change[i];
		if (i <= p) {
			ans = std::max(ans, i-1);
		}
	}
	printf("%d\n", ans);
	return 0;
}

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

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::pair<int,int> PII;

typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;
typedef std::vector<PII> VPII;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

int N;
VI cnt(100005, 0);
int main() {
    std::cin >> N;

    int a, b;
    REP(i, N) {
        std::cin >> a >> b;
        FOR(i, a, b+1) {
            ++cnt[i];
        }
    }

    int ans = 0;
    FOR(i, 2, 100002) {
        if(i-1 <= cnt[i]) {
            ans = i-1;
        }
    }
    std::cout << ans << std::endl;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n),b(n);
    for(int i = 0; i < n; i++)cin >> a[i] >> b[i];
    int ok = 0, ng = n+1;
    int t = 20;
    while(t--){
        int mid = (ok + ng)/2;
        int cnt = 0;
        for(int i = 0; i < n; i++){
            if(a[i] <= mid+1 && mid+1 <= b[i])cnt++;
        }
        //cout << ok << " " << ng << " " << mid << " " << cnt << endl;
        if(cnt >= mid)ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
}

C++: #include <stdio.h>

int main(void) {
  int i, j, n, sum[100010], a, b, now = 0, ans = 0;
  for(i = 0; i < 100010; ++i) sum[i] = 0;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%d%d", &a, &b);
    sum[a]++;
    sum[b + 1]--;
  }
  /*for(i = 0; i < 10; ++i) printf("%d ", sum[i]);
  printf("\n");*/
  for(i = 1; i < 100010; ++i) {
    now += sum[i];
    if(now + 1 >= i) ans = i - 1;
  }
  if(ans < 0) ans = 0;
  printf("%d\n", ans);
  return 0;
}