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 vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int cnt[100010];
signed main(void)
{
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    cnt[a]++;
    cnt[b]--;
  }
  FOR(i, 1, 100001) cnt[i] += cnt[i-1];
  //REP(i, 10) cout << cnt[i] << " "; cout << endl;
  int ret = 1;
  FOR(i, 2, n+1) {
    //cout << cnt[i] << " " << i << endl;
    if(cnt[i] >= i-1) {
      ret = i-1;
    }
  }
  cout << ret << endl;
  return 0;
}

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;

    int r[100003]={0};
    rep(i,n)
    {
        int a,b;
        scanf(" %d %d", &a, &b);
        ++r[a];
        --r[b+1];
    }

    rep(i,100002) r[i+1]+=r[i];

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

    for(int i=n; i>0; --i)
    {
        if(r[i+1]>=i)
        {
            ans=i;
            break;
        }
    }

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

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

#define fs first
#define sc second
typedef pair<int,int> P;

int n;
P p[200100];

int main(){
  cin >> n;
  for(int i=0;i<n;i++)cin >> p[i].fs >> p[i].sc;
  p[n] = P(1,100001);
  sort(p,p+n+1);

  priority_queue<int, vector<int>, greater<int> > q;
  int ans = 1;

  for(int i=0;i<=n;i++){
    if(q.empty())q.push(p[i].sc);
    else if(p[i].fs <= q.top())q.push(p[i].sc);
    else{
      while(!q.empty() && q.top() < p[i].fs)q.pop();
    }    
    if(p[i].fs <= q.size() && q.size() <= q.top())ans = q.size();
  }

  cout << ans-1 << 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 INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

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

int n;

bool check(VVI& table, int number) {
    // number テ」ツ?ッティツ?ェテ・ツ按?」ツつづ・ツ青ォテ」ツつ?」ツつ?
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (table[i][0] <= number && table[i][1] >= number) {
            count++;
        }
    }
    if (count >= number - 1) {
        return true;
    } else {
        return false;
    }
}

int main(void) {
    cin >> n;
    VVI table(n, VI(2));
    for (int i = 0; i < n; i++) {
        cin >> table[i][0] >> table[i][1];
    }
    int left = 1;
    int right = n + 1;

    while (left < right) {
        int middle = (left + right) / 2; // leftテ」ツつ暗」ツつ?
        if (check(table, middle) == true) {
            left = middle + 1;
        } else {
            right = middle;
        }
    }
    cout << left - 2 << endl; // leftテ」ツ??テ」ツ?、テ・ツ、ツァテ」ツ?催」ツ??+ ティツ?ェテ・ツ按?」ツつ津ヲツ環愿」ツ??
/*
    while (left < right) {
        int middle = (left + right) / 2 + 1; // rightテ」ツつ暗」ツつ?
        if (check(table, middle) == true) {
            left = middle;
        } else {
            right = middle - 1;
        }
    }
    cout << left - 1 << endl; // ティツ?ェテ・ツ按?」ツつ津ヲツ環愿」ツ??
*/

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const long long INF = 1e7+1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector < pair<ll, ll > > vp;
typedef vector <string> vs;
typedef vector <char> vc;
typedef list <ll> lst;

ll n, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/


int main()
{
	ll a, b;
	cin >> n;
	vi va, vb;
	REP(i, n)
	{
		cin >> a >> b;
		va.push_back(a);
		vb.push_back(b);
	}
	sort(ALL(va));
	sort(ALL(vb));
	if (va[va.size() - 1] - 1 > n) {
		cout << 0 << endl;
		return 0;
	}
	if (vb[0] - 1 > n) {
		cout << 0 << endl;
		return 0;
	}
	cout << max(va[va.size() - 1] - 1, vb[0] - 1) << endl;
}

C++: #include <iostream>

using namespace std;

const int MAX = 100002;
 
int main(){
    int n,ans[MAX],a,b;
 
    fill(ans,ans+MAX,0);
 
    cin >> n;
    while(n--){
	cin >> a >> b;
	ans[a]++; ans[b+1]--;
    }
 
    for(int i = 1 ; i < MAX ; i++)
	ans[i] += ans[i-1];
 
    int fri = 0;
    for(int i = 1 ; i < MAX ; i++){
	if(ans[i]+1 >= i) fri = i-1; 
    }
    cout << fri << endl;
    return 0;
}

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

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

  int res = 0;
  for(int i=1;i<=N+1;i++){
    D[i] = D[i]+D[i-1];
    if( D[i] >= i ) res = i;
  }
  cout << res-1 << endl;
}

C++: #include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 100002> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	//cout << 1 << endl;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		if(friends[i] >= ans && friends[i] - i >= 0){
			ans = max(ans, friends[i]);
			if(ans - 1 == i)ans++;
		}
		//cout << friends[i] << endl;
	}

	//cout << ans << endl;
	ans = max(0, ans - 2);
	cout << ans << endl;
	return 0;
}

C++: #include <stdio.h>

int n, x, y, imos[100002];

int main()
{
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &x); x--;
        scanf("%d", &y);
        
        imos[x]--;
        imos[y]++;
    }
    
    int sum = 0;
    
    for(int i = 100000; i >= 1; i--)
    {
        sum += imos[i];
        
        if(sum >= i)
        {
            printf("%d\n", i - 1);
            
            return 0;
        }
    }
    
    printf("0\n");
    
    return 0;
}