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++: /*
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {
  int n;
  cin >> n;
  vi a(n), b(n);
  int maxn = 0;
  REP(i, n){
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    maxn = max(b[i], maxn);
  }
  maxn++;

  SORT(a);
  SORT(b);

  auto ita = a.begin();
  auto itb = b.begin();

  int res = 0;
  int cnt = 0;

  REP(i, maxn){
    while(*ita == i) {
      ita++;
      cnt++;
    }
    while(*itb == i - 1) {
      itb++;
      cnt--;
    }

    if (i <= cnt) {
      res = i;
    }
  }
  cout << res << 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-1]++;
    imos[b]--;
  }
  REP(i,1,n+1){
    imos[i]+=imos[i-1];
    if(imos[i]==n)ans=i;
  }
  if(ans>0)printf("%d\n",ans);
  else printf("0\n");
  return 0;
}

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

int main()
{
  int N;
  cin >> N;
  int c[N+2] = {};
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    c[a]++;
    c[b+1]--;
  }
  int ans = 0;
  int sum = 1;
  for(int i = 0; i < N+2; i++){
    sum += c[i];
    ans = (i <= sum) ?  i : ans;
  }
  cout << ans - 1 << endl;
}

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

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

//////
using pii = pair<int,int>;

bool isok(vector<pii> &ab, int n){
  int acc = 0;
  for(auto&& x:ab){
    if(x.first <= n && n <= x.second)acc++;
  }
  return acc>=n;
}

int main(){
  int n;
  cin >> n;
  vector<pii> ab(n);
  for(auto&& x:ab)cin >> x.first >> x.second;
  int res = 0;bool f = false;
  for(int i=1;i<=n;i++){
    if(isok(ab,i+1)){
      f = true;
      res = i;
    }
  }
  cout << res << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>
#include<queue>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin >> n;
	int sum[100010] = {};
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		sum[a - 1]++;
		sum[b]--;
	}
	int ans = 0;
	for (int i = 1; i < n; i++) {
		sum[i] += sum[i - 1];
	}
	for (int i = 0; i <= n; i++) {
		if (sum[i] >= i) {
			ans = i;
		}
	}
	cout << ans << endl;
	getchar();
	getchar();
	return	0;
}

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

using namespace std;

int n;

int main()
{
    cin >> n;
    vector< vector<long long> > ab(n + 2, vector<long long>(0) );
    vector<long long> away(n + 2, 0);
    int a,b;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        if(a < n + 2) ab[a].push_back(b);
    }
    sort(ab.begin(), ab.end());
    int count = 1;
    int m = 1;
    for(int j = 1; j < n + 2; j++){
        if(ab[j].size() == 0) continue;
        if(count + ab[j].size() < j){
            count = count + ab[j].size();
            for (int k = 1; k <= ab[j].size(); k++)
            {
                away[(int)ab[j][k - 1] + 1]++;
            }
            continue;
        }
        for(int k = 1; k <= ab[j].size(); k++){
            count++;
            count -= away[count];
            //cout << count << endl;
            m = max(m, count);
            away[(int)ab[j][k - 1] + 1]++;
        }
    }
    //for(int k = 0; k < n + 2; k++) cout << away[k] << endl;
    cout << m - 1 << endl;
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

int n;
int a[100000], b[100000];
int imos[100100];

int solve() {
	int cnt = 1;
	int ret = 0;
	for(int i = 1; i <= 100010; i++) {
		cnt += imos[i];
		if(cnt >= i) {
			ret = i - 1;
		}
	}
	return ret;
}

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

	while(cin >> n) {
		memset(imos, 0, sizeof imos);
		for(int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
			imos[a[i]]++;
			imos[b[i] + 1]--;
		}
		cout << solve() << endl;
	}
}

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

int main() {
  int N, a, b, res;
  signed int invite[100000] = {0};

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

  signed int sum = 0;
  for(int i = 0; i < N; i++) {
    sum += invite[i];
    if(i == sum)
      res = i;
  }
  cout << res << endl;
}

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

using std::cin;
using std::cout;
using std::endl;

struct data_t {
	int min;
	int max;

	bool operator<(const data_t& right) const {
		if ( min == right.min ) {
			return  ( ( max < right.max ) || ( max == right.max ) );
		}
		else {
			return ( min < right.min );
		}
	}
};

int main(void) {
	int N;
	cin >> N;
	std::vector<data_t> fri(N);
	for (int i = 0; i < N; i++) {
		cin >> fri[i].min >> fri[i].max;
	}
	std::sort(fri.begin(), fri.end());

	int ret = 0;
	int count;
	bool end = false;
	for (int f = 2; f < N+2; f++) {
		count = 1;
		end = false;
		for (int i = 0; i < N && !(end); i++) {
			if ( fri[i].min <= f  && fri[i].max >= f ) {
				count++;
			}
			else {
				end = true;
			}
		}

		if ( count != 1 && count > ret ) {
			if ( count >= f ) {
				ret = f-1; 
			}
		}
	}
	cout << ret << endl;
	return 0;
}