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>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=1; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=1; i<MAX; i++) {
    if(dp[i]+1>=i) mx = i;
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long

const int MAX = 100001;

int n;
int a[MAX], b[MAX];

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int imos[MAX + 2];
  fill(imos, imos + MAX + 2, 0);
  imos[0]++;
  for (int i = 0; i < n; i++) {
    imos[a[i]]++;
    imos[b[i] + 1]--;
  }

  /*
  for (int i = 1; i <= 10; i++) {
    cout << imos[i] << " ";
  }
  cout << endl;
  */
  for (int i = 1; i < MAX + 2; i++) {
    imos[i] += imos[i - 1];
  }
  for (int i = n + 1; i >= 2; i--) {
    if (imos[i] >= i) {
      cout << i - 1 << endl;
      /*
  for (int i = 1; i <= 10; i++) {
    cout << imos[i] << " ";
  }
  cout << endl;
  */
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

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

int main()
{
  int n;
  int num[100000][2];
  int maxa,minb;
  int i,j,k;
  int indexa,indexb;

  cin>>n;

  for(i=0;i<n;i++)
    cin>>num[i][0]>>num[i][1];

  for(k=0;k<n;k++){

    maxa=num[0][0];
    minb=num[0][1];
    indexa=indexb=0;
    for(i=1;i<n;i++){
      if(maxa<num[i][0]){
	maxa=num[i][0];
	indexa=i;
      }
      if(minb>num[i][1]){
	minb=num[i][1];
	indexb=i;
      }
    }
    if(maxa<=n-k+1 && n-k+1<=minb){
      cout<<n-k<<endl;
      break;
    }else if(maxa<=n-k+1){
      num[indexb][0]=0;
      num[indexb][1]=100001;
    }else if(minb>=n-k+1){
      num[indexa][0]=0;
      num[indexa][1]=100001;
    }else{
      num[indexa][0]=0;
      num[indexa][1]=100001;
      num[indexb][0]=0;
      num[indexb][1]=100001;
      k++;
    }
  }
  if(k>n-2)
    cout<<"0"<<endl;
}

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

int n;
int v[100010];
int ans;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	cin >> n;
	for(int i=0; i<n; ++i){
		int a, b;
		cin >> a >> b;
		++v[a];
		--v[b+1];
	}
	for(int i=1; i<=n+1; ++i){
		v[i] += v[i-1];
		if(v[i]+1>=i)
			ans = max(ans, i-1);
	}
	cout << ans << "\n";
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int sum[100100];

int main() {
	cin >> n;
	fill(sum, sum + 100100, 0);
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	int ans = 0;
	REP(i, n) {
		sum[i + 1] += sum[i];
	}
	FOR(i, 1, n + 2) {
		//cout << sum[i];
		if (i - 1 <= sum[i]) {
			ans = max(ans, i - 1);
			//if (sum[i] >= i)ans = max(ans, i - 1);
			//else ans = max(ans, i);
		}
		//else if (i - 1 == sum[i])ans = max(ans, i - 1);
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    c[a]++;
    c[b]--;
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  for(int i=0;i<10;i++){
    cout << c[i] << endl;
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i;
  }
  cout << maxnum-1 << endl;
  return 0;
}

C++: //#include <bits/stdc++.h>
//#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <bitset>
#include <algorithm>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <string.h>
#include <cmath>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <cctype>
#include <fstream>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000			 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define DINF 1000000000000000.0
#define PI 3.14159265358979
#define MAXI 7500000

//.size()はunsigned int　0-1をするとオーバーフローする

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

//int dy[5] = { 0,0,1,-1,0 };
//int dx[5] = { 1,-1,0,0,0 };

int dy[5] = {-1, 0, 1, 0, 0};
int dx[5] = {0, -1, 0, 1, 0};


int main(void)
{
	long long int n;
	long long int a, b;
	vector<long long int> dp(100002, 0);
	int maxi = -1;

	cin >> n;
	FOR(i,1,n){
		cin >> a >> b;
		dp[a]++;
		dp[b + 1]--;
	}
	FOR(i,1,100001){
		dp[i] += dp[i - 1];
	}


	FOR(i, 1, 100001)
	{
		if(dp[i]+1>=i){
			maxi = i - 1;
		}
	}

	cout << maxi << endl;
}

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



int main() {
	int N;
	int num[100001];

	int sum;


	cin >> N;

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

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



	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int main() {
  int N; cin >>N;
  vector<int> v(100010, 0);
  REP(i, N){
    int a, b; cin >>a >>b;
    v[a]++; v[b + 1]--;
  }
  FOR(i, 1, v.size()) v[i] += v[i - 1];
  int ans = 1;
  REP(i, v.size()) if(i <= v[i] + 1) ans = max(ans, v[i]);
  cout <<ans - 1 <<endl;
  return 0;
}