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;
  for(int i = 0; i < N; i++){
    p++;
    if(v[i].a <= p && p <= v[i].b){
      continue;
    }else if(v[i].a > p){
      p--;
      if(N - i < v[i].a - 1){
        break;
      }else{
        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){
            cout << p - 1 << endl;
            return 0;
          }
        }
        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>
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]>=i-1){
      ans=i;
    }
  }
  cout<<ans-1<<endl;
  return 0;
}

C++: #include <iostream>
#define MAX 100000
using namespace std;

int range[MAX][3];

int search(int n) {
  int number = n + 1;
  int flag = 1;
  while (flag) {
    flag = 0;
    for (int i = 0; i < n; i++) {
      if ((number < range[i][0] || range[i][1] < number) && !range[i][2]) {
	flag = 1;
	number--;
	range[i][2] = 1;
      }
    }
  }

  return number - 1;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> range[i][0] >> range[i][1];
    range[i][2] = 0;
  }
  
  int ans = search(n);
  cout << ans << endl;
    
  return 0;
}

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

int main(){

    int N;
    cin >> N;
    vector<int> cnt(100005, 0);

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

    vector<int> acc(100005);
    acc[0] = 0;
    for(int i = 1; i < N + 3; i++){
        acc[i] = acc[i - 1] + cnt[i];
    }

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

    return 0;
}

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int cnt[100010];
signed main(void)
{
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    cnt[a]++;
    cnt[b+1]--;
  }
  FOR(i, 1, 100001) cnt[i] += cnt[i-1];
  //REP(i, 10) cout << cnt[i] << " "; cout << endl;
  int ret = 0;
  FOR(i, 0, n+2) {
    //cout << cnt[i] << " " << i << endl;
    if(cnt[i] >= i-1) {
      ret = i-1;
    }
  }
  cout << ret << endl;
  return 0;
}

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

static const int MAX = 100000+100;

int main(){
    int N;
    cin>>N;
    vector<int> a(N);
    vector<int> b(N);
    vector<int> V(MAX,0);

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

    int ans = 0;
    int sum = 1; //自分を入れる

    for(int i=0; i<MAX; i++){
        sum += V[i];
        if(i > sum) continue;
        ans = i;
    }

    cout<<ans-1<<endl;

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 200000;

struct SegSum {
  int bit[MAX_N], n;
  SegSum(int n) : n(n) {
    memset(bit, 0, sizeof(bit));
  }
  int sum(int i) {              // [1,i]テ」ツ?ョテ・ツ陳?
    int s = 0;
    while(i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  int sum2(int a, int b) {      // [a,b]テ」ツ?ョテ・ツ陳?
    return sum(b)-sum(a-1);
  }
  void add(int i, int x) {      // i(>=1)テ」ツ?ォxティツカツウテ」ツ??
    while(i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  void addSeg(int a, int b, int x) {   // [a,b]テ」ツつ遅テ・ツ?凝・ツ環?」ツ?暗」ツつ?
    add(a, x);
    add(b+1, -x);
  }
};

struct StarrySky {
  SegSum ss1, ss2;
  StarrySky(int n) : ss1(n), ss2(n) {}
  int sum(int i) {
    return ss1.sum(i) + ss2.sum(i) * i;
  }
  int sum2(int a, int b) {
    return sum(b) - sum(a-1);
  }
  void addEach(int a, int b, int x) {
    ss1.add(a, -x*(a-1));
    ss1.add(b+1, x*b);
    ss2.addSeg(a, b, x);
  }
};


int main() {
  int n;
  cin >> n;
  StarrySky bit(100002);
  REP(i,n) {
    int a, b;
    cin >> a >> b;
    bit.addEach(a,b,1);
  }
  int ans = 0;
  for (int i=1; i<=100000; ++i) {
    int s = bit.sum2(i+1,i+1);
    // if (i < 10)
    //   cout << i << " "<< s << endl;
    if (s >= i) ans = max(ans, i);
  }
  cout << ans << endl;
}

C++: #include <iostream>

struct Range {
	int beg;
	int end;
}; 

int main(void){
	int n;
	std::cin >> n;

	Range* ps = new Range[n];

	for(int i=0; i<n; i++) std::cin >> ps[i].beg >> ps[i].end;

//	i???"?????????"??????????????°
	int mps = 1;
	for(int i=1; i<=n+1; i++){
		int cps = 0;
		bool f = false;
		for(int j=0; j<n; j++){
			if(i >= ps[j].beg && i <= ps[j].end){
				cps ++;
				if(cps >= i-1){
					f = true;
					break;
				}
			}
		}
		if(f) mps = i;
	}

	std::cout << (mps-1) << '\n';

	return 0;
}

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=0;
  for(int i=0;i<n+1;i++){
    now+=rui[i];
    if(now>i)
    ma=max(ma,now);
  }
  cout<<ma<<endl;
  return 0;
}

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

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