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 <algorithm>
#include <vector>
using namespace std;

struct data{
  int a, b;
};
  
bool comp(const data &x, const data &y)
{
  if(x.b != y.b) return x.b > y.b;
  else return x.a < y.a;
}

int main()
{
  int N;
  cin >> N;
  vector<data> v(N);
  for(int i = 0; i < N; i++) cin >> v[i].a >> v[i].b;
  sort(v.begin(),v.end(),comp);

  int p = 1;
  bool flag;
  for(int i = 0; i < N; i++){
    p++;
    flag = true;
    if(v[i].a <= p && p <= v[i].b){
      continue;
    }else if(v[i].a > p){
      p--;
      for(int j = i + 1; j < i + v[i].a - 1; j++){
        if(v[j].a > v[i].a || v[i].a > v[j].b){
          i += v[i].a - 3;
          flag = false;
          break;
        }
      }
      if(flag){
        p += v[i].a - 1;
        i += v[i].a - 2;
      }
    }else if(v[i].b < p){
      p--;
      break;
    }
  }
  cout << p - 1 << endl;
}

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

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)

int main(void) {
    int n; cin >> n;
    vector<int> a(150000);
    rep(i, n) {
        int l, r; cin >> l >> r;
        l--;
        a[l]++;
        a[r]--;
    }
    rep(i, a.size()-1) {
        a[i+1] += a[i];
    }
    int ret = 0;
    rep(i, a.size()) {
        if (a[i] >= i) 
            ret = i;
    }
    cout << ret << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    c[a]++;
    c[b]--;
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i;
  }
  cout << maxnum << endl;
  return 0;
}

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

int main(){
  int n;
  std::cin >> n;
  vector<int> f(1e6 + 1, 0);
  int a, b, ans = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a >> b;
    f[a - 1]++;
    f[b]--;
  }
  for (int i = 1; i <= 1e6; i++)f[i] += f[i - 1];
  for (int i = 0; i <= 1e6; i++) {
    ans = max(ans, (i <= f[i])?i:0);
  }
  std::cout << ans << std::endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
using namespace std;
int main(){
    int n;
    int cnt[100100];
    memset(cnt,0,sizeof(cnt));
    cin>>n;
    REP(i,n){
        int a,b;
        cin>>a>>b;
        cnt[a]++;cnt[b+1]--;
    }
    int ans=0;
    REP(i,100000)cnt[i+1]+=cnt[i];
    REP(i,n+2){
        if(cnt[i]+1>=i){
            ans=i-1;
        }
    }
    cout<<ans<<endl;
}

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

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VB vector<bool>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

template<class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> void print(T x) { std::cout << x << std::endl; }

const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = { 0, -1, 0, 1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<int> dp(100003, 0);
    FOR(i, 0, N) {
        int a, b;
        cin >> a >> b;
        dp[a] += 1;
        dp[b + 1] -= 1;
    }

    FOR(i, 0, dp.size() - 1) {
        dp[i + 1] += dp[i];
    }

    int ans = 0;
    FOR(i, 2, N + 1) {
        ans = max(ans, min(dp[i], i) - 1);
    }

    print(ans);

    return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
	int n, f[100000][2];
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> f[i][0] >> f[i][1];
	}
	for (int i = n + 1; i >= 2; i--){
		int friends = 0;
		for (int j = 0; j < n; j++){
			if (f[j][0] <= i && f[j][1] >= i){
				friends++;
			}
		}
		if (friends >= i - 1){
			cout << i - 1 << endl;
			return 0;
		}
	}
	cout << 0 << 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]++;
    }
  }

  for(int i=0;i<10005;i++){
    if(i<num[i]+1)
      m=max(m,i-1);
  }
  cout<<m<<endl;
}

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

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

typedef std::vector<int>  VI;

int N;
VI imos(100002, 0);
int main() {
    std::cin >> N;

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

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

C++: #include <iostream>
using namespace std;
int n,i,a[100002],b,c,m,j;
int main(){
  for(cin>>n;i<n;i++){
    cin>>b>>c;
    for(j=b;j<=c;j++)a[j]++;
  }
  for(i=0;i<n+2;i++)if(a[i]+1>=i)m=i;
  cout<<m-1<<endl;
}

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



int main() {
	int N;
	int num[100001];

	int sum;


	cin >> N;

	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		for (int j = a; j <= b; j++) {
			num[j]++;
		}
	}

	
	for (int i = N + 1; i > 0; i--) {
		if (num[i] >= i - 1) {
			cout << i - 1;
			break;
		}
	}



	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>
#include<queue>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin >> n;
	int sum[100010] = {};
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		sum[a - 1]++;
		sum[b]--;
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		sum[i] += sum[i - 1];
	}
	for (int i = 0; i <= n; i++) {
		if (sum[i] >= i) {
			ans = i;
		}
	}
	cout << ans << endl;
	return	0;
}

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 = 100002;
    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;
}