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>
using namespace std;

#define SUPERFRIENDNUM 100000
#define AIDA 100

unsigned int friendInfo[SUPERFRIENDNUM][2];

unsigned int CheckAida(unsigned int maxFriend, unsigned char count, unsigned int N) {
	unsigned int i = maxFriend + 2;
	unsigned int okPeople = 0;
	for (unsigned int j = 0; j < N && okPeople<i; j++) {
		if (friendInfo[j][0] <= i && friendInfo[j][1] >= i) {
			okPeople++;
		}
	}
	if (okPeople >= i - 1) {
		count++;
		maxFriend = i - 1;
		if (count < AIDA) {
			maxFriend = CheckAida(maxFriend, count, N);
		}
	}
	return maxFriend;
}


int main() {
	//データ入力部
	unsigned int N = 0;
	cin >> N;
	unsigned int min = SUPERFRIENDNUM+1;
	unsigned int max = 1;
	unsigned int minMax = 1;
	for (unsigned int i = 0; i < N; i++) {
		cin >> friendInfo[i][0] >> friendInfo[i][1];
		if (min > friendInfo[i][0]) {
			min = friendInfo[i][0];
		}
		if (minMax < friendInfo[i][0]) {
			minMax = friendInfo[i][0];
		}
		if (max < friendInfo[i][1]){
			max = friendInfo[i][1];
		}
	}
	if (max > N + 1) {
		max = N + 1;
	}

	//計算部
	unsigned int maxFriend = 0;
	unsigned int okPeople = 0;
	//Max何人まで許されるかな
	for (unsigned int i = min; i <= max; i+=AIDA) {
		//皆に聞いていく
		okPeople = 0;
		for (unsigned int j = 0; j < N && okPeople<i; j++) {
			if (friendInfo[j][0] <= i && friendInfo[j][1] >= i) {
				okPeople++;
			}
		}
		if (okPeople >= i - 1) {
			maxFriend = i - 1;
		}
		if (minMax < i && okPeople < i - 1) {
			break;
		}
	}
	maxFriend = CheckAida(maxFriend, 0, N);
	

	cout << maxFriend << endl;
}

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

int N;
int num[100002];

bool input() {
  int i, j, a, b;

  scanf("%d", &N);

  for (i = 0; i < N; i++) {
    scanf("%d %d", &a, &b);
    for (j = a; j <= b && j <= N + 1; j++) {
      num[j]++;
    }
  }

  return true;
}

int main() {
  int i, sum;

  input();
  for (i = N + 1; i > 0; i--) {
    if (num[i] >= i - 1) {
      break;
    }
  }
  printf("%d\n", i - 1);



  return 0;
}

C++: #include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 100000> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		ans = max(ans, friends[i]);
	}

	cout << max(ans - 2, 0) << endl;

}

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

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;


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

  int n;cin>>n;
  int arr[100010]={};
  arr[1]=1;
  for(int i=0;i<n;i++){
      int a,b;cin>>a>>b;
      arr[a]++;arr[b+1]--;
  }
  for(int i=1;i<100010;i++){
      arr[i]+=arr[i-1];
  }
  for(int i=100000;1<=i;i--){
      if(arr[i]>=i){
          cout<<i-1<<endl;
          return 0;
      }
  }
  cout<<0<<endl;
  return 0;
}

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]);
    }
    if (ans) ans--;
    std::cout << ans << std::endl;
}

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 = 100000, range_max = 0;
	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 + 1 <= imos[i])
		{
			ninzu_max = i + 2;
		}
	}
	std::cout << ninzu_max - 1 << std::endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

int main(void){
    int p;
    cin>>p;
    vi d(100002,0);
    d[0]=1;
    rep(lp,p){
        int a,b; cin>>a>>b;
        d[a]++,d[b+1]--;
    }
    int ans=0;
    range(i,1,100002){
        d[i]+=d[i-1];
        if(d[i]>=i) ans=i;
    }
    cout<<ans-1<<endl;
}

C++: #include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
int c[100002];
int main() {
	int a, b, n, sum = 0;
	while (cin >> n) {
		int mab = 0;
		fill(c, c + 100002, 0);
		for (int z = 0; z < n; z++) {
			cin >> a >> b;
			for (int i = a; i <= b; i++) {
				c[i]++;
				mab = max(mab, c[i]);
			}
		}
		for (int i = mab; i >= 1; i--) {
			if (i >= c[i]) {
				cout << --i << endl;
				goto stop;
			}
		}
		cout << 0 << endl;
	stop:;
	}
}

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++: #include <iostream>
using namespace std;

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

	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;
  pair<int,int>fri[n];
  for(int i=0;i<n;i++)cin>>fri[i].F>>fri[i].S;
  int le=0,ri=100100;
  while(ri-le>1){
    int mid=(le+ri)/2;
    int cnt=1;
    for(int j=0;j<n;j++){
      if(fri[j].F<=mid&&mid<=fri[j].S)cnt++;
    }
    if(cnt>=mid)le=mid;
    else ri=mid;
  }
  cout<<le-1<<endl;
  return 0;
}