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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int sum[100100];

int main() {
	cin >> n;
	fill(sum, sum + 100100, 0);
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a - 1]++;
		sum[b]--;
	}
	int ans = 0;
	REP(i, n) {
		sum[i + 1] += sum[i];
	}
	FOR(i, 1, n + 1) {
		//cout << sum[i];
		if (i < sum[i])ans = max(ans, i);
	}
	cout << ans << endl;
}

C++: #include <iostream>
using namespace std;
int n,a,b;
int member[100004];
int sum;;

int main(){
	member[1]=1;
	cin >>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		member[a]++;
		member[b+1]--;
	}
	sum=0;
	for(int i=1;i<=n;i++){
		member[i+1]+=member[i];
		if(i+1<=member[i+1])sum=i;
	}
	cout<<sum <<endl;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define VMAX 1e05 + 3

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int main(void) {
    VI total(VMAX, 0);
    VI start(VMAX, 0);
    VI goal(VMAX, 0);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int s;
        int g;
        cin >> s >> g;
        start[s]++;
        goal[g + 1]++;
    }
    total[0] = 0;
    for (int i = 1; i <= n + 1; i++) {
        total[i] = total[i-1] + start[i] - goal[i];
    }
    int rmax = 0;
    for (int i = n + 1; i >= 1; i--) {
        if (total[i] + 1 >= i) {
            cout << i - 1 << endl;
            break;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=1<<29,MOD=1e9+7;

int main(){
	int n; cin>>n;
	int a[100010]={};
	for(int i=0;i<n;i++){
		int x,y; cin>>x>>y;
		a[x]++; a[y+1]--;
	}
	int ans=0;
	for(int i=0;i<100010;i++){
		a[i+1]+=a[i];
		if(i<=a[i]&&ans<i) ans=i;
	}
	cout<<--ans<<endl;
	return 0;
}

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

int A[1000010];
int N, ans;

int main() {
	cin >> N;
	for (int i = 0; i < 1000003; i++) A[i] = 0;
	for ( int n = 0; n < N; n++) {
		int a,b;
		cin >> a >> b;
		A[a] += 1;
		A[b + 1] -= 1;
	}
	ans = 0;
	for (int i = 1; i < 1000002; i++) {
		A[i] += A[i - 1];
	}
	
	for (int i = 1; i < 1000002; i++) {
		if (i <= A[i + 1]) {
			ans = i;
		}
	}
	cout << ans << endl;
}

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>
#include <map>
#include <algorithm>

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

int n, a, b, friends[100005], ans;

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

    std::cin >> n;
    rep(i,n) {
        std::cin >> a >> b;
        if (a > n+1) {
            n--;
            continue;
        } else if (b > n+1) {
            b = n+1;
        }
        for (int i = a; i <= b; ++i) friends[i]++;
    }
    rep(i,n+1) {
        ans = std::max(ans, friends[i]);
    }
    ans = std::max(ans, 1);
    std::cout << ans-1 << std::endl;
}

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

int N;


int main() {
    cin >> N;
    int memo[101010];
    fill(memo, memo+101010, 0);
    for (int j = 0; j < N; ++j) {
        int a, b;
        cin >> a >> b;
        ++memo[a];
        --memo[b+1];
    }
    int ret = 0;
    for (int j = 2; j < 101010; ++j) {
        memo[j] += memo[j-1];
        if (memo[j] >= j-1) {
            ret = max(ret, j-1);
        }
    }
    cout << ret << 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=0; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=0; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include <iostream>

using namespace std;

int mymax(int a, int b){
  return (a > b) ? a : b;
}


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

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

  int ans[N+1];
  for(int i = 0 ; i < N + 1 ; i++){
    ans[i] = 0;
  }

  for(int i = 0 ; i < N ; i++){
    for(int j = a[i] ; j <= b[i] && j <= N ; j++){
      if(ans[j] < j){
        ans[j]++;
      }else{}
    }
  }

  int max = -1;
  for(int i = 0 ; i < N+1 ; i++){
    if(max < ans[i]){
      max = ans[i];
    }
  }

  cout << mymax(max - 1,0) << endl;

}