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;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int main()
{
    int n;
    cin >>n;
    vector<int> a(n),b(n);
    rep(i,n) scanf(" %d %d", &a[i], &b[i]);

    //number of people to invite
    int ans=0;

    for(int num=n; num>0; --num)
    {
        int ct=0;
        rep(i,n)
        {
            if(a[i]<=num+1 && num+1<=b[i]) ++ct;
        }

        if(ct>=num)
        {
            ans=num;
            break;
        }
    }

    printf("%d\n", ans);
    return 0;
}

C++: #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cout << "[" << __LINE__ << "] ";
#define dump(i) cout << #i ": " << i << " ";
#define dumpl(i) cout << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    map<int, int> imos;
    rep(i, n)
    {
        int a, b;
        cin >> a >> b;
        imos[a] += 1;
        imos[b] -= 1;
    }
    int ans = 0;
    int c = 0;
    rep(i, n + 1)
    {
        c += imos[i];
        if (c + 1 >= i)
            ans = max(ans, c);
    }
    cout << ans << endl;
    return 0;
}

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

#define rep(i,j,n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define LINF 1e18
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

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

  int n;
  cin >> n;
  vt<int> imos(100100, 0);
  rep(i, 0, n) {
    int a, b;
    cin >> a >> b;
    imos[a]++; imos[b + 1]--;
  }

  rep(i, 0, 1e5) imos[i + 1] += imos[i];

  for(int i = 1e5 + 1; i >= 1; --i) {
    if(imos[i] + 1 >= i) return cout << i - 1 << endl, 0;
  }
}

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

//#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,x,...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__,REP1,REP2,REP3))(__VA_ARGS__)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond) if(cond) break; else

template<typename T>
inline void input(vector<T>& v) { for (auto& x : v) cin >> x; }

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

const int INF = 1 << 29;

int s[100004];
signed main() {
	int n;
	while (cin >> n && n) {
		memset(s, 0, sizeof(s));
		rep(i, n) {
			int a, b; cin >> a >> b;
			s[a]++;
			s[b + 1]--;
		}
		rep(i, n + 2) s[i + 1] += s[i];
		cout << *max_element(s, s + n + 1) - 1 << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <array>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>

#define MAX 100005
using namespace std;


int main(void)
{
	int table[MAX];

	int i, j;
	int n;
	int temp1, temp2;
	int max = 0;
	int count = 0;
	bool ha[MAX];

	cin >> n;

	vector <int> a, b;


	for (i = 0; i < n; i++) {
		cin >> temp1 >> temp2;
		a.push_back(temp1);
		b.push_back(temp2);
		
	}

	for (i = 0; i < MAX; i++) {
		table[i] = 0;
	}

	sort(a.begin(), a.end());


	if (n == 1 && a[0] == b[0] && a[0] == 2) {
		cout << 1 << endl;
		return (0);
	}


	for (i = 0; i < n; i++) {
		if (a[i] > n + 1) {
			a[i] = MAX + 2; //????????????????????????????????????
		}
		else {
			count++;
		}

		if (count + 1 < a[0]) {
			cout << max << endl;
			return (0);
		}

	}





	for (i = 0; i < n; i++) {
		table[a[i]]++;
		table[b[i]]--;

	}

	for (i = 1; i < MAX; i++) {
		table[i] = table[i] + table[i - 1];

	}

	for (i = 0; i < MAX; i++) {
		if (max < table[i]) {
			max = table[i];
		}
	}

	cout << max << endl;


	return (0);
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(){
  int num[100005]={0};
  int n,m=0;
  cin>>n;
  int a,b;
  num[0]++;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    //for(int j=a;j<=b;j++){
    //num[j]++;
    //}
    num[a]++;
    num[b+1]--;
  }

  for(int i=2;i<10005;i++){
    num[i]+=num[i-1];
    if(num[i]+1>=i){
      cout<<i-1<<endl;
    }
  }
  //for(int i=10004;i>0;i--){
  //if(num[i]>=i){
  //  cout<<i-1<<endl;
  //  return 0;
  //}
  //}
}