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<cstdio>

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

using namespace std;

int main(){
	int n; scanf("%d",&n);
	int a[100000],b[100000];
	rep(i,n) scanf("%d%d",a+i,b+i);

	int lo=0,hi=n;
	while(lo<hi){
		int mi=(lo+hi+1)/2;
		int cnt=0;
		rep(i,n) if(a[i]<=1+mi && 1+mi<=b[i]) cnt++;
		if(mi<=cnt) lo=mi;
		else        hi=mi-1;
	}
	printf("%d\n",lo);

	return 0;
}

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

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

int main() {
	
	ll n; 
	cin >> n;

	map<ll, int> mp;	

	rep(i, n){
		ll a, b;
		cin >> a >> b;
		mp[a]++;
		mp[b]--;
	} 
	
	int num = 1;
	int ans = 1;
	for(auto it = mp.begin(); it != mp.end(); it++){
		//cout << it->second << endl;
		num += it->second;
		if(num >= it->first) ans = max(ans, num);
	}
	
	cout << ans - 1 << endl;
	

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin()nnd()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main(){
    int n,f[100010]={}; cin>>n;
    rep(i,n){
        int a,b; cin>>a>>b;
        f[a]++,f[b+1]--;
    }
    int ans=0;
    rep(i,100005){
        f[i+1]+=f[i];
        if(f[i]>=i-1){
            ans=max(ans,i-1);
        }
    }
    cout<<ans<<endl;
    return 0;
}

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

using namespace std;

int a[100002], b[100002];
int cumsum[100003];

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

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

	memset(cumsum, 0, sizeof(cumsum));
	for(int i=0;i<N;i++)
	{
		cumsum[a[i]] += +1;
		cumsum[b[i] + 1] -= 1;
	}
	int ans = 0;
	for(int i=1;i<=N+1;i++)
	{
		cumsum[i] += cumsum[i - 1];
		if(cumsum[i] >= i - 1)
		{
			ans = i;
		}
	}
	cout << ans - 1 << endl;

	return 0;
}

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

int a[100000] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c]++;a[b+1]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i-1 <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l]++;
        result[r + 1]--;
    }
    int ans = 0;
    for(int i = 1; i < n; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] + 1 >= i) ans = max(i + 1, ans);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y && i<=n;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+2){
    if(a[i]>=i) ans=i;
  }
  if(ans==0) cout<<0<<endl;
  else 
  cout<<ans-1<<endl;
  return 0;
    
}