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>

#define CEIL(a,b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))

using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'000'000'001;
constexpr ll LLINF = 4'000'000'000'000'000'001;
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int n;
    cin >> n;
    vector<int> t(100002);
    for(int i = 0; i < n; ++i){
        int a, b;
        cin >> a >> b;
        --a; --b;
        t[a] += 1;
        t[b+1] -= 1;
    }
    for(int i = 0; i <= n+1; ++i){
        t[i+1] += t[i];
    }
    for(int i = n + 1; i >= 0; --i){
        if(t[i] >= i){
            cout << i << "\n";
            return 0;
        }
    }
    cout << "0\n";
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  for (int i = 0; i < N; i++){
    cin >> a[i] >> b[i];
  }
  vector<int> d(100003, 0);
  for (int i = 0; i < N; i++){
    d[a[i]]++;
    d[b[i] + 1]--;
  }
  for (int i = 0; i < 100002; i++){
    d[i + 1] += d[i];
  }
  int ans = 0;
  for (int i = 1; i < 100003; i++){
    if (d[i] + 1 >= i){
      ans = i - 1;
    }
  }
  cout << ans << endl;
}

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

int num[100005];
int num2[100005];

int main(){
    int N,a,b;
    cin>>N;
    for(int n=0;n<N;n++){
        cin>>a>>b;
        // cout<<a<<' '<<b<<endl;
        num[a]++;
        num[b+1]--;
    }
    int ans=num2[0]=num[0];
    for(int i=1;i<100005;i++){
        num2[i]=num2[i-1]+num[i];
        if(num2[i]>=i) ans=max(ans,num2[i]);
    }
    ans--;
    cout<<max(0,ans)<<endl;
}

C++: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n;

main(){
	while(cin >> n){
		vector<int> im(100002);
		REP(i, n){
			int x, y;
			cin >> x >> y;
			im[x--] ++;
			im[y] --;
		}
		int ans = 0;
		REP(i, n+1){
			im[i+1] += im[i];
			if(im[i+1] >= i) ans = i;
		}
//		cout << im << endl;
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
    REP(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll n; cin >> n;
    map<ll, ll> m;
    REP(i, n) {
        ll a, b; cin >> a >> b;
        m[a]++;
        m[b+1]--;
    }
    ll ans = 1;
    ll cnt = 1;
    EACH(p, m) {
        ll need, d; tie(need, d) = p;
        cnt += d;
        if (cnt >= need) {
            // cout << need << " " << d << " " << cnt << endl;
            ans = max(ans, need);
        }
    }
    cout << ans-1 << endl;
}

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

using namespace std;

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

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
typedef pair<int,int> P;

int main(){
  int n;
  while(cin>>n){
    vector<int> a(n);
    vector<int> b(n);
    REP(i,n)cin>>a[i]>>b[i];
    for(int i = n; i>=0; i--){
      int count = 0;
      REP(j,n)if(a[j]<=i+1 && i+1<=b[j]){
        count++;
      }
      if(count >= i){
        cout<<i<<endl;
        return 0;
      }
    }
  }
  return 0;
}