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

int main(){
  int N, a, b, ans, s, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  sort(A.begin(),A.end());
  sort(B.begin(),B.end());
  ans = 0;
  vector<int>::iterator itr1, itr2;
  for (int i = N; i >= 0; --i) {
    itr1 = upper_bound(A.begin(),A.end(),i+1);
    itr2 = lower_bound(B.begin(),B.end(),i+1);
    s = distance(A.begin(), itr1) + 1;
    t = distance(B.begin(), itr2);
    if (min(s,N-t) == i + 1) {
      ans = min(s, N-t) - 1;
      break;
    }
  }
  cout << ans << endl;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(1000010);
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    ++b;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,100001){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,1,n+1){
    k = max(k,ans[i]); 
  }
  if(k == 1) cout << 0 << endl;
  else
    cout << k-1 << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int ll
typedef pair<int, int> pii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i=(b-1);i>=(a);--i)
#define REP(i,n)   FOR(i,0,n)
#define RREP(i,n)   RFOR(i,0,n)

const int size = 100010;
int cum[size];

signed main() {
    ios::sync_with_stdio(false);cin.tie(0);
    int N; cin >> N;
    REP(_, N) {
        int a, b; cin >> a; cin >> b;
        cum[a] += 1;
        cum[b+1] += -1;
    }
    REP(i, size-1) {
        cum[i+1] += cum[i];
    }
    REP(i, 8) {
    }
    RREP(i, size) {
        if (cum[i]+1 >= i) {
            cout << i-1 << endl;
            return 0;
        }
    }
    cout << 0 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int N,a,b;
  int max=0;
  int ans=1;
  int man[100001];
  for(int i=0;i<100001;i++){
    man[i]=0;
  }
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++) man[j]++;
  }
  for(int i=2;i<=N+1;i++){
    if(man[i]>=max && man[i]>=i-1){
      max=man[i];
      ans=i;
    }
  }
  cout<<ans-1<<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 = 100003;
    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) && (m + 1) <= P[i].second) {
                count++;
            }
        }
        if (m <= count) {
            l = m;
        } else {
            r = m;
        }
    }

    cout << l << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100001] = {0}, ans = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--; b--;
            range(j,a,b + 1){
                count[j]++;
            }
        }
        rep(i,n + 1) if(count[i] >= i && count[i] > ans) ans = i;
        cout << ans << endl;
    }
}

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

using namespace std;

static int member[100003];

void solve()
{
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    member[a]++;
    member[b + 1]--;
  }
  partial_sum(begin(member), end(member), begin(member));
  for(int i = 1e6 + 1; i > 0; i--) {
    if(member[i] + 1 >= i) {
      cout << i - 1 << "\n";
      return;
    }
  }
}

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

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const static int tx[] = {0,1,1,1,0,-1,-1,-1};
const static int ty[] = {-1,-1,0,1,1,1,0,-1};
 
static const double EPS = 1e-8;

class Condition{
public:
  int lower;
  int upper;
  Condition(int _l,int _u) : lower(_l), upper(_u) {}
  bool operator<(const Condition& c) const{
    if(lower != c.lower){
      return lower < c.lower;
    }else{
      return upper < c.upper;
    }
  }
  bool operator>(const Condition& c) const{
    if(lower != c.lower){
      return lower > c.lower;
    }else{
      return upper > c.upper;
    }
  }
};

int main(){
  int N;
  while(~scanf("%d",&N)){
    priority_queue<Condition,vector<Condition>,greater<Condition> > ppl;
    for(int i=0;i<N;i++){
      int lower,upper;
      scanf("%d %d",&lower,&upper);
      ppl.push(Condition(lower,upper));
    }
    int current = 1;

    int res = 0;
    int dp[100001];
    memset(dp,0,sizeof(dp));

    while(!ppl.empty()){
      Condition c = ppl.top();
      ppl.pop();
      if(current + 1 >= c.lower){
        current++;
        current -= dp[current];
        dp[c.upper + 1]++;
        res = max(current - 1,res);
      }
    }

    printf("%d\n",res);
  }
}

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;
  num[0]++;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    //for(int j=a;j<=b;j++){
    //num[j]++;
    //}
    num[a]++;
    num[b+1]--;
  }

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