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++: // template {{{

#include <bits/stdc++.h>

// #define int long long

#define GET_MACRO(a, b, c, d, NAME, ...) NAME
#define REP2(i, n) REP3(i, 0, n)
#define REP3(i, a, b) REP4(i, a, b, 1)
#define REP4(i, a, b, s) for (int i = (a); i < (int)(b); i += s)
#define RREP2(i, n) RREP(i, 0, n)
#define RREP3(i, a, b) for (int i = (b) - 1; i >= (int)(a); i--)

#define rep(...) GET_MACRO(__VA_ARGS__, REP4, REP3, REP2)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = std::vector<int>;
using vvi = std::vector<vi>;

template<typename T>
using maxpq = std::priority_queue<T, std::vector<T>, std::less<T>>;

template<typename T>
using minpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
inline T sq(T x){ return x * x; }

template<typename T, typename U>
inline bool chmax(T &x, U y){ if (x >= y) return false; x = y; return true; }

template<typename T, typename U>
inline bool chmin(T &x, U y){ if (x <= y) return false; x = y; return true; }

template<typename T>
inline void sort(T &c){ std::sort(std::begin(c), std::end(c)); }

template<typename T>
inline void reverse(T &c){ std::reverse(std::begin(c), std::end(c)); }

template<typename T>
inline void unique(T &c){ std::sort(std::begin(c), std::end(c)); c.erase(std::unique(all(c)), std::end(c)); }

using namespace std;

// }}}

int n;
int a[100010];

int main()
{
	cin >> n;
	rep(i, n){
		int x, y;
		cin >> x >> y;
		a[x]++; a[++y]--;
	}
	rep(i, 100002) a[i + 1] += a[i];
	int res = 0;
	rep(i, 100002){
		if (a[i] >= i - 1) res = i - 1;
	}
	cout << res << endl;
}

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

int ruisekiwa[100005];
int main()
{
	int a; cin >> a;
	for (int b = 0; b < a; b++) {
		int c, d; scanf("%d%d", &c, &d);
		ruisekiwa[c]++; ruisekiwa[d + 1]--;
	}
	int k = 1;
	int MAX = 0;
	for (int i = 0; i < 100005; i++) {
		k += ruisekiwa[i];
		if (k >= i)MAX = max(MAX, min(k, i));
	}
	cout << MAX-1 << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;

typedef long long ll;

#define INF 10e17
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto itr : x) { debug(itr); }

template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;}
template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;}

static constexpr int MAX = 100001;

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

  cout << mmax << endl;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define VMAX 1e05 + 3

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int main(void) {
    VI total(VMAX, 0);
    VI start(VMAX, 0);
    VI goal(VMAX, 0);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int s;
        int g;
        cin >> s >> g;
        start[s]++;
        goal[g + 1]++;
    }
    total[0] = 0;
    for (int i = 1; i < 1e05 + 3; i++) {
        total[i] = total[i-1] + start[i] - goal[i];
    }
    int rmax = 0;
    for (int i = 1; i <= n + 1; i++) {
        rmax = max(rmax, min(total[i], i) - 1);
    }
    cout << rmax << endl;
    return 0;
}

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

int n,a,b;
int A[100002]={},B[100002]={};

int main()
{
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    A[a]++;
    B[b+1]++;
  }
  int sum=1,ans=0;
  for(int i=1;i<100002;i++){
    sum+=(A[i]-B[i]);
    //if(i<=10)cout<<i<<" "<<sum<<endl;
    if(i<=sum)ans=i;
  }
  cout<<ans-1<<endl;
  return 0;
}

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

using namespace std;

int main(){
	int n;
	cin >> n;
    vector<pair<int , int> > invert(n);

    int nmax=-10,nmin=(int)1e7;
    for(int i = 0; i < n; i++){
        int a , b;
        cin >> a >> b;
        invert[i].first = b;
        invert[i].second = a;

        nmin = min(nmin,a);
        nmax = max(nmax,b);
        //cout << nmax << " " << nmin << endl;
    }
    sort(invert.begin() , invert.end());
    nmax = min(nmax , n);
    int ans=0;

    for(int i = nmax; i >= nmin-1; i--){
        vector<pair<int , int> >::iterator up;
        pair<int , int> mp(i+1,0);
        up = lower_bound(invert.begin() , invert.end() , mp);
        int lower = distance(invert.begin() , up);
        int c = 0;
        //cout << invert[lower].first << " " << invert[lower].second << endl;
        for(int j = lower; j < invert.size(); j++){
            if(invert[j].second <= i+1){
                c++;
            }
        }
        if(c >= i){
            ans = i;
            break;
        }
    }
    cout << ans << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
#define N 100002
using namespace std;
int n,a,b,d[N],ans;

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