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;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
int N;
vector<int> event;

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    event.resize(100003);
    for(int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        event[a]++;
        event[b+1]--;
    }
    int ans = 1;
    int now = 0;
    for(int i = 1; i <= 1e5 + 2; i++) {
        now += event[i];
        if(now >= i - 1) ans = i - 1;
    }
    cout << ans << endl;
    return 0;
}

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]++;
    imos[b+1]--;
  }

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

  cout<<ans-1<<endl;
}

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int MOD=1e9+7;
const int INF=1e9;
const double ESP=1e-5;
template<typename T> void cmax(T &a, T b) {a = max(a, b);}
template<typename T> void cmin(T &a, T b) {a = min(a, b);}

int d[100005];
signed main(){
  int n;
  cin>>n;
  rep(i,n){
    int a,b;
    cin>>a>>b;
    a--;
    d[a]++;
    d[b]--;
  }
  int ans=0;
  rep(i,100000)d[i+1]+=d[i];
  loop(i,1,100000)if(d[i]>=i)ans=i;
  cout<<ans<<endl;
 return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,h[111111]={};
	cin>>n;
	int a,b;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		h[a]++;
		h[b+1]--;
	}
	int ma=0;
	for(int i=1;i<=n+1;i++){
		h[i]+=h[i-1];
		if(i<=h[i]+1)ma=max(ma,i-1);
	}
	cout<<ma<<endl;
	return 0;
}

C++: #include <cstdio>

#define FRIEND_NUM 100001
int c[FRIEND_NUM+1];
int main(){
  int a,b,N,i;

  scanf("%d",&N);
  for(i=0; i<N; ++i){
    scanf("%d %d",&a,&b);
    for(; a<=b; ++a){
      ++(c[a]);
    }
  }
  for(a=N+1; a>=2;){
    --a;
    if(c[a+1] >= a){
      printf("%d\n",a);
      break;
    }
  }
}

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 << endl;
	}
	cout << ret << endl;
	return 0;
}

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);
  }

  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 <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main(){
  int n;
  cin >> n;
  vector<int> fri(100002,0);
  int first,end,max_friend=0;
  REP(i,n){
    cin >> first >> end;
    fri[first]++;
    fri[end+1]--;
  }

  for(int i = 1;i<100001;i++){
    fri[i] +=fri[i-1];
    if(fri[i]  >= i-1)
      max_friend = max(i-1,max_friend);
  }
  cout << max_friend << endl;
  
  return 0;
}

C++: #include<cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int n; scanf("%d",&n);
	int imos[100003]={};
	rep(i,n+1){
		int l,r;
		if(i<n) scanf("%d%d",&l,&r);
		else    l=0, r=100001;
		imos[l]++;
		imos[r+1]--;
	}

	int ans,sum=0;
	rep(i,n+2){
		sum+=imos[i];
		if(i<=sum) ans=i-1;
	}
	printf("%d\n",ans);

	return 0;
}