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>
int main()
{
	int imos[100001];
	for(int i = 0; i < 100001; i++)
	{
		imos[i] = 0;
	}
	int N;
	std::cin >> N;
	int range_min = 0, range_max = 100000;
	for(int i = 0; i < N; i++)
	{
		int a, b;
		std::cin >> a >> b;
		imos[a - 2]++;
		imos[b - 1]--;
		if(range_min > a - 2)
		{
			range_min = a - 2;
		}
		if(range_max < b - 1)
		{
			range_max = b - 1;
		}
	}
	for(int i = range_min; i < range_max; i++)
	{
		imos[i + 1] += imos[i];
	}
	int ninzu_max = 1;
	for(int i = range_min; i < range_max; i++)
	{
		if(i + 2 <= imos[i])
		{
			ninzu_max = i + 2;
		}
	}
	std::cout << ninzu_max - 1 << std::endl;
	return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
typedef pair<int, int> pii;

int N, A, B;
int s[200000];

int main(void) {
  cin >> N;
  REP(i, 0, N + 2) s[i] = 0;
  REP(i, 0, N) {
    cin >> A >> B;
    s[A - 1]++;
    s[B]--;
  }
  REP(i, 1, 100001) s[i] += s[i - 1];

  int ans = 0;
  REP(i, 0, N + 2) if(i <= s[i]) ans = i;
  cout << ans << endl;

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cassert>
using namespace std;
int main(){
  int n,st,ed;
  int info[100100][2],total,lim; // 0 -> st,1 -> ed
  while(cin >> n){
    total = 1; lim = 1;
    for(int i=0;i<100100;i++)info[i][0] = info[i][1] = 0;
    for(int i=0;i<n;i++){
      cin >> st >> ed; lim = max(lim,ed);
      info[st][0]++;
      info[ed][1]++;
    }
    int ans = 0,prev = -1;
    for(int i=1;i<=lim;i++){
      if(info[i][0] != 0 && total+1 >= i)total+=info[i][0];
      //cout << "pre-total = " << total << "    " << "prev = " << prev << "    ";
      if(prev!=-1 && info[prev][1] != 0 && total > prev){
	info[prev][1] = total-prev;
	//cout <<"total-prev = " << total-prev <<" -A- ";
	total -= (total-prev); ;
      }
      if(info[i][1] != 0 && total > i){
	total -= (total-i);//cout << " -B- ";
      }
      else if(info[i][1] != 0)prev = i;
      //cout<< i << "-th total = " << total << endl;
      assert(total>=0);
      ans = max(ans,total);
    }

    cout << ans-1 << endl;

  }
  return 0;
}

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

using namespace std;

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

    int n, a, b;

    cin >> n;
    vector<int> sum(n + 3, 0);
    for (int i  = 0; i < n; ++i) {
        cin >> a >> b;
        if (a <= n + 1)
            ++sum[a - 1];
        if (b <= n + 1)
            --sum[b];
    }

    for (int i = 1; i <= n; ++i)
        sum[i] += sum[i - 1];

    int ans = 0;
    for (int i = 0; i <= n; ++i)
        if (i <= sum[i])
            ans = max(ans, i);

    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;num++){
		int now=before+left[num]-right[num-1];
		if(now>=num) ans=max(ans,num-1);
		before=now;
	}
	cout<<ans<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);++i)
#define rep(i,n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;
int imos[100010];
int main() {
    int N; scanf("%d", &N);
    int a, b;
    memset(imos, 0, sizeof(imos));
    rep(i, N) {
        scanf("%d%d", &a, &b);
        imos[a]++; imos[b + 1]--;
    }
    rep(i, N + 1) imos[i + 1] += imos[i];
    rep(i, N + 1) if(imos[i] < i - 1) imos[i] = 0;
    int ans = 0;
    rep(i, N + 1) ans = max(ans, imos[i]);
    printf("%d\n", max(ans - 1, 0));
    return 0;
}

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

int main(){
  int N, a, b, temp[100101] = {}, ma = 0;

  cin>>N;

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

  temp[0] = 1;

  for(int i = 1; i <= 100101; i++){
    temp[i]+= temp[i-1];
    if(temp[i] >= i){
      ma = i;
    }
    // cout<<temp[i]<<" ";
  }

  cout<<ma-1<<endl;
  
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int main(){
	int n;
	int a[100000], b[100000];

	cin >> n;
	int max = 0, min = 100001;
	for (int i = 0; i < n; ++i){
		cin >> a[i] >> b[i];
		if (a[i] < min) min = a[i];
		else if (b[i] > max) max = b[i];
	}

	int max_friends = 0;
	for (int i = min; i < max+1; ++i){
		int friends = 0;
		for (int j = 0; j < n; ++j){
			if (a[j] <= i && b[j] >= i){
				++friends;
				cout << i << a[j] << b[j] << "OK" << endl;
			}
			else{
				cout << i << a[j] << b[j] << "NG" << endl;

			}
		}
		if ((friends-1) == i){
			max_friends = i;
		}
	}

	cout << max_friends << endl;



	return 0;
}

C++: #include <cstdio>

#define FRIEND_NUM 100001
int c[FRIEND_NUM+1];
int main(){
  int a,b,N,i;

  scanf("%d",&N);
  for(i=0; i<N; ++i){
    scanf("%d %d",&a,&b);
    for(; a<=b; ++a){
      ++(c[a]);
    }
  }
  for(a=N+1; a>=2;){
    --a;
    if(c[a+1] >= a){
      printf("%d\n",a);
      break;
    }
  }
}

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]={};
	a[1]++;
	rep(i,n){
		int x,y; cin>>x>>y;
		a[x]++; a[y+1]--;
	}
	int ans=0;
	rep(i,100010){
		a[i+1]+=a[i];
		if(i<=a[i]&&ans<a[i]) ans=i;
		//cout<<a[i]<<endl;
	}
	cout<<--ans<<endl;
	return 0;
}

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int main() {
	int N;
	cin >> N;
	
	vector< int > imos(100010, 0);
	for_(i,0,N) {
		int a, b;
		cin >> a >> b;
		imos[a-1]++;
		--imos[b];
	}
	
	for_(i,0,100010) imos[i+1] += imos[i];
		
	int ans = 0;
	for_(i,0,100010) if (i <= imos[i]) ans = i;
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>

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

using namespace std;

const int INF = 1<<29;

int x[100010];
int main(){
	int n;
	cin >> n;
	rep(i, n){
		int a, b;
		cin >> a >> b;
		x[a]++, x[b+1]--;
	}

	int num = 1;
	int best = 0;
	rep(i, 100010){
		num += x[i];
		if(num >= i){
			best = max(best, i-1);
		}
	}
	cout << best << 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]--;
	}
    rep(i,6){
        cout << imos[i] << "\n";
    }
    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";
}