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;

int main()
{
  int N;
  cin >> N;
  int c[N+3] = {};
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    c[a]++;
    c[b+1]--;
  }
  int ans = 0;
  int sum = 1;
  for(int i = 0; i < N+3; i++){
    sum += c[i];
    ans = (i <= sum) ?  i : ans;
  }
  cout << ans - 1 << endl;
}

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

using namespace std;

static int member[100004];

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] >= i - 1) {
      cout << i - 1 << "\n";
      return;
    }
  }
}

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

C++: #include <iostream>
#include <algorithm>
#define N 100000
int a[N],b[N],c[N];
using namespace std;
int main( void )
{
  int i,j,k,n,d=0,ret=0;
  cin >> n;
  for( i=0; i<n; i++ )
    cin >> a[i] >> b[i];
  sort( a, a+n );
  sort( b, b+n );
  for( i=2,j=0,k=0; i<=n+1; i++ ){
    while( a[j] == i ){ j++; d++; }
    while( b[k] == i-1 ){ k++; d--; }
    if( i -1 <= d ) ret = i-1;
  }
  cout << ret << endl;
  return 0;
}

C++:     #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
     
    int main() {
    	int N;
    	vector<int> F;
    	
    	cin>>N;
    	for(int i=0;i<N+1;i++){
    		F.push_back(0);
    	}
     
    	for(int i=0;i<N;i++){
    		int x,y;
    		cin>>x>>y;
     		
     		int xn=max(0,x-1);
     		if(x<=N){
    		F[xn]++;
     		}
    		if(y<=N){
    			F[y]--;
    		}
    	}
     
    	for(int i=0;i<N;i++){
    		F[i+1]+=F[i];
    	}
     
    	int ans;
    	for(int i=0;i<N+1;i++){
    		if(F[i]>=i){
    			ans=i;
    		}
    	}
     
    	cout<<ans<<endl;
     
    	return 0;
    }

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

	int imos[100005];
	memset(imos, 0, sizeof(imos));

	rep(i, n) {
		int a, b;
		cin >> a >> b;

		imos[a]++;
		imos[b+1]--;
	}

	REP(i, 1, 100005) {
		imos[i] += imos[i-1];
	}

	int ans = 0;
	rep(i, 100005) {
		if(imos[i+1] >= i) {
			ans = i;
		}
	}

	cout << ans << endl;

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int solve(){
    int res = 0;
    int N; cin >> N;
    vector<int> imos(100010,0);
    for(int i = 0; i < N;i++){
        int a,b; cin >> a >> b;
        imos[a]++; imos[b+1]--;
    }
    for(int i = 1; i <= N+1;i++){
        imos[i] += imos[i-1];
        
        if(imos[i] >= i-1) res = i-1;
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}

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

using namespace std;

static vector<int64_t> nums(100002, 0);

int32_t main() {
  uint32_t N;
  cin >> N;

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

  uint32_t max_val = 0;
  for (uint32_t i = 0; i < nums.size() - 1; i++) {
    nums[i + 1] += nums[i];
    if (i + 1 <= nums[i + 1] + 1) {
      max_val = max(max_val, i + 1);
    }
  }

  cout << max_val - 1 << endl;

  return 0;
}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

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

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

int main()
{
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    const int MAX = 1e5 + 1;
    int sum[100010] = {};
    sum[0]++;
    sum[MAX+1]--;

    rep(i, N) {
        int A, B;
        cin >> A >> B;
        sum[A]++;
        sum[B+1]--;
    }

    rep(i, MAX) {
        sum[i+1] += sum[i];
    }

    int ans = 0;
    rep(i, MAX) {
        if (sum[i] >= i) {
            ans = i;
        }
    }

    cout << ans - 1 << endl;
}