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 <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 + 1) {
		sum[i + 1] += sum[i];
	}
	FOR(i, 1, n + 2) {
		if (i - 1 <= sum[i]) {
		//cout << 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 <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int main(){
  int ans=0;
  int n,a,b;
  int imos[100005]={0};
  cin>>n;
  rep(i,n){
    scanf("%d%d",&a,&b);
    imos[a]++;
    imos[b+1]--;
    //cout<<imos[a]<<" "<<imos[b+1]<<endl;
  }
  REP(i,1,n+1){
    imos[i]+=imos[i-1];
    if(imos[i]>=ans)ans=imos[i]-1;
  }
  if(ans>0)printf("%d\n",ans);
  else printf("0\n");
  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//??????????????§????????§??????????????§?????????

int main(void){
	int N;
	cin>>N;
	int num[100001]={};
	int a,b,min=999999,max=-1,ans=0;
	rep(i,N){
		cin>>a>>b;
		reg(i,a,b)num[i]++;
		if(a<min)min=a;
		if(b>max)max=b;
	}
	reg(i,min,max){
		if(num[i]+1>=i)ans=i-1;
	}

	cout<<ans<<endl;
	return 0;
}

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

int N;
int a,b,ans=0;
int P[100010]={};

int main(){
  cin >> N;
  for(int i=0;i<N;i++){
    cin  >> a >> b;
    for(int j=a-1;j<=b-1;j++){
      P[j]+=1;
    }
  }
  for(int i=0;i<=N+1;i++){
    //cout << P[i] << endl;
  }
  for(int i=1;i<=N+1;i++){
    //cout << P[i] << endl;
    //ans = max(ans,P[i]);
    if(P[i]>=i){
      ans = max(ans,i);
    }
    else if(P[i]==i){
      ans = max(ans,P[i]);
    }
    //cout << i <<":" << ans << endl;
  }
  //if(ans!=0) ans-=1;
  cout << ans << endl;
}

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

using namespace std;

int main(void){
  int n;
  cin>>n;
  int a,b;
  int dp[100010] = {0};
  for(int i = 0 ; i < n ; i ++){
    cin>>a>>b;
    dp[a]++;
    dp[b+1]--;
  }
  int ret = 0;
  for(int i = 0 ; i < 100009 ; i ++){
    dp[i+1] += dp[i];
    if(i-1<=dp[i])ret = max(ret,i-1);
  } 
  cout<<ret<<endl;
}

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-1;
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<functional>
using namespace std;

#define INF 1<<21
#define MOD 1000000007

int main(void) {
  int n;
  int a, b;
  cin >> n;
  int ans[100005];
  fill(ans, ans + 100005, 0);
  for (int i = 0; i < n; i++) {
	cin >> a >> b;
	if (a >= n + 1)continue;
	ans[a]++;
	ans[b + 1]--;
  }

  int m = 0;
  for (int i = 1; i <= 100005; i++) {
	ans[i] += ans[i-1];
  }
  for (int i = 1; i <= 100005; i++) {
	//cout << ans[i] << endl;
	if (ans[i]+1 >=i) {
	  m = i-1;
	}
  }
  if (m < 0)m = 0;
  cout << m << endl;
}