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>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  vector<pair<int,int> >pii;
  int rui[100010]={};
  for(int i=0;i<n;i++){
    int a,b;cin>>a>>b;a-=2;b-=2;
    rui[a]++;
    rui[b+1]--;
  }
  int ma=0;
  int now=1;
  for(int i=0;i<n+1;i++){
    now+=rui[i];
    if(now>i)
    ma=max(ma,now);
  }
  cout<<ma-1<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int a[MAX_N],b[MAX_N];
int n;
int imos[MAX_N];
int sch[MAX_N];

int main()
{
	scanf("%d",&n);
	rep(i,n){
		scanf("%d%d",&a[i],&b[i]);
        imos[a[i]]++;
        imos[b[i]+1]--;
    }
    sch[0] = imos[0];
    rep(i,100002){
        sch[i+1] = sch[i] + imos[i+1];
    }
    int res = -1;
    rep(i,100002){
        if(sch[i] >= i-1 && i > res){
            res = i;
        }
    }
    cout << res - 1 << "\n";
}

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;
int main() {
    std::cin >> N;
    imos.resize(N+2, 0);

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

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

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100002;

int main(){
  int n,ans[MAX],a,b;
  int MX = -1<<27;
  int MN = 1<<27;
  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    MX = max(MX,b);
    MN = min(MN,a);
    for(int i = a ; i <= b ; i++)
      ++ans[i];
  }
  int fri = 0;
  for(int i = MN+1 ; i <= MX+1 ; i++){
    if(ans[i] > i-1) fri = i-1; 
  }
  cout << fri << endl;

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF (1<<28)
#define N (1<<17)

int h[100010];
int main(){
	int n; cin >> n;
	
	for(int i = 0 ; i < n ; i++){
		int a,b;
		cin >> a >> b;
		a--,b--;
		h[a]++;
		h[b+1]--;
	}
	int ans = 0 , cur = 0;
	for(int i = 0 ; i <= 100001 ; i++){
		cur += h[i];
		//if(cur) cout << i << " " << cur << endl;
		if(cur >= i) ans = max(ans,i);
	}
	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, 0, N + 1) {
        ans = max(ans, min(dp[i], i));
    }

    print(ans - 1);

    return 0;
}

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

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	vi v(100003);
	int n;
	cin >> n;
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		v[b]--;
		v[a - 1]++;
	}
	int ans = 0;
	REP(i, v.size() - 1)v[i + 1] += v[i];
	REP(i, v.size()) {
		if (i + 1 == v[i])ans = i + 1;
	}
	cout << max(0,ans-1) << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX = 1e6+2;

int main(){
	int n;
	cin >> n;
	
	vector<int> nf(MAX, 0);
	for(int i=0; i<n; i++){
		int a,b;
		cin >> a >> b;
		nf[a]++;
		nf[b+1]--;
	}
	
	int ans=0;
	for(int i=2; i<MAX; i++){
		nf[i] += nf[i-1];
		if(nf[i]+1>=i) ans=i-1;
	}
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;

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

	int left[100002],right[100002];
	memset(left,0,sizeof(left));
	memset(right,0,sizeof(right));

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

	int before=left[2];
	int ans=0;
	if(left[2]>=1) ans=1;

	for(int num=3;num<=N+1;num++){
		int now=before+left[num]-right[num-1];
		if(now>=num-1) ans=max(ans,num-1);
		before=now;
	}
	cout<<ans<<endl;
}

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

int n,N;
int seg[300100],p[100100][2];

void init(int n){
  N = 1;
  while(N<n)N <<= 1;
  for(int i=0;i<2*N;i++)seg[i] = 0;
}

void add(int a, int b, int k, int l, int r){
  if(b<=l || r<=a)return;
  if(a<=l && r<=b)seg[k]++;
  else{
    add(a,b,2*k+1,l,(l+r)/2);
    add(a,b,2*k+2,(l+r)/2,r);
  }
}

int sum(int v){
  v += N-1;
  int res = seg[v];
  while(v>0){
    v = (v-1)/2;
    res += seg[v];
  }
  return res;
}
    
  

int main(){
  cin >> n;
  for(int i=0;i<n;i++)cin >> p[i][0] >> p[i][1];
  p[n][0] = 1; p[n][1] = 100001;

  init(100001);

  for(int i=0;i<=n;i++){
    add(p[i][0],p[i][1]+1,0,0,N);
  }

  int ans = 1;
  for(int i=0;i<=n+1;i++){
    if(i<=sum(i))ans = i;
  }

  cout << ans-1 << endl;
}