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 <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<pair<int,int> > P(N);
    for (auto &i : P) cin >> i.first >> i.second;

    int l = 0, r = N + 1;
    while (r - l > 1) {
        int m = (r + l) / 2;
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (P[i].first > m + 1 || P[i].second < m + 1) {
                count++;
            }
        }
        if (m <= N - count) {
            l = m;
        } else {
            r = m;
        }
    }

    cout << l << endl;

    return 0;
}

C++: #include <stdio.h>

int main(void) {
  int i, j, n, sum[100010], a, b, now = 0, ans = 0;
  for(i = 0; i < 100010; ++i) sum[i] = 0;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%d%d", &a, &b);
    sum[a]++;
    sum[b + 1]--;
  }
  /*for(i = 0; i < 10; ++i) printf("%d ", sum[i]);
  printf("\n");*/
  for(i = 1; i < 100010; ++i) {
    now += sum[i];
    if(now + 1 >= i) ans = now - 1;
  }
  if(ans < 0) ans = 0;
  printf("%d\n", ans);
  return 0;
}

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

using namespace std;

typedef pair<int,int> pii;
typedef vector<int> vi;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999



int main(){
    int n;
    cin>>n;
    int memo[100010]={};
    rep(i,n){
        int a,b;
        cin>>a>>b;
        memo[a]++;
        memo[b+1]--;
    }
    int ans=0;
    rep(i,100009){
        memo[i+1]=memo[i+1]+memo[i];
    }
    rep(i,100009){
        if(memo[i]+1>=i){
            ans=max(ans,i-1);
        }

    }
    cout<<ans<<endl;
}

C++: #include <iostream>

using namespace std;

int mymax(int a, int b){
  return (a > b) ? a : b;
}


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

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

  int ans[N+2];
  for(int i = 0 ; i < N + 2 ; i++){
    ans[i] = 1;
  }

  for(int i = 0 ; i < N ; i++){
    for(int j = a[i] ; j <= b[i] && j < N + 2 ; j++){
      if(ans[j] < j){
        ans[j]++;
      }else{}
    }
  }

  int max = -1;
  for(int i = 0 ; i < N + 2 ; i++){
    if(max < ans[i] && i <= ans[i]){
      max = ans[i];
    }
  }

  for(int i = 0 ; i < N + 2 ; i++){
    cout << i << ":" << ans[i] << endl;
  }

  cout << mymax(max - 1,0) << endl;

}

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


int e[100002];

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		e[a]++;
		e[b+1]--;
	}
	int ans=0,c=0;
	for(int i=1;i<=100001;i++){
		c+=e[i];
		if(i<=c+1)ans=i-1;
	}
	cout<<ans<<endl;
	return 0;
}

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

using namespace std;

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

  int n;
  cin>> n;
  int come[120000]={0};
  come[0]++;
  for(int i=0;i<n;i++){
    int a,b;
    cin >> a>>b;
    come[a]++;
    come[b+1]--;
  }
  for(int i=1;i<120000;i++) come[i]+=come[i-1];
  int res=1;
  for(int i=2;i<120000;i++){
    if(i<=come[i]) res=i;
  }
  cout << res-1 << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
typedef pair<int,int> pi;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

void solve(int n){
  int a,b;
  vector<int> imos(100005,0);
  for(int i=0;i<n;i++){
    cin>>a>>b;
    imos[a-1]++;
    imos[b]--;
  }

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

  cout<<ans<<endl;
}

int main(){
  int n;
  while(cin>>n){
    solve(n);
  }
  return 0;
}

C++: #include <stdio.h>

int main(void) {
  int i, j, n, sum[100010], a, b, now = 0, ans = 0;
  for(i = 0; i < 100010; ++i) sum[i] = 0;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%d%d", &a, &b);
    sum[a]++;
    sum[b]--;
  }
  /*for(i = 0; i < 10; ++i) printf("%d ", sum[i]);
  printf("\n");*/
  for(i = 1; i < 100010; ++i) {
    now += sum[i];
    if(now + 1 >= i) ans = now;
  }
  printf("%d\n", ans);
  return 0;
}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>

int change[100010] = {};
int main() {
	int N = 0;
	scanf("%d",&N);
	for (int i = 0; i < N;i++) {
		int a, b;
		scanf("%d %d",&a,&b);
		change[a]++;
		change[b+1]--;
	}
	int ans=0;
	int p=0;
	for (int i = 2; i <= N + 1; i++) {
		p += change[i];
		if (i-1 <= p) {
			ans = std::max(ans, i-1);
		}
	}
	printf("%d\n", ans);
	return 0;
}

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

#define rep(i,x,y) for(int (i)=(x);(i)<(y);(i)++)
#define INF 1000000000

vector<int> A;
vector<int> B;

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

  vector<int> F(N+1,0);
  rep(i,0,N){
    int a,b;
    cin>>a>>b;
    a--;
    if(a<N){
    F[a]++;
    }
    if(b<N){
      F[b]--;
    }
  }
  rep(i,0,N){
    F[i+1]+=F[i];
  }

  int ans=0;
  rep(i,0,N+1){
    if(F[i]>i){
      ans=max(ans,i);
    }
  }

  cout<<ans<<endl;

  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    int sum[100010]={};
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        sum[a]++;
        sum[b+1]--;
    }
    for(int i=0;i<100003;i++){
        sum[i+1]+=sum[i];
    }
    int ans=0;
    for(int i=0;i<=n;i++){
        int t=min(i-1,sum[i]);
        ans=max(ans,t);
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include"bits/stdc++.h"
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main()
{
	int n;
	cin >> n;
	vector<pii> v;
	vi s(111111);
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		s[a-1]++; s[b]--;
		v.emplace_back(a, b);
	}
	FOR(i, 1, 111110) s[i] += s[i - 1];
	int ans = 0;
	REP(i, 111111)
	{
		if (s[i] >= i) chmax(ans, i);
	}
	cout << ans << endl;
	return 0;
}