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 <iostream>
#include <vector>

using namespace std;
	

int main(){
	int N;
	int maxn = 0;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;

		if(humans[i] < nokemono[i]) nokemono[i] = humans[i];
		if(humans[i] < i){
			//cout << i << endl;
			nokemono[i] = 0;
		}
		if(nokemono[i] > maxn) maxn = nokemono[i];
		//if(nokemono[i] < humans[i]) nokemono[i] = 0;
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
		//cout << i << "," << humans[i]-1 << endl;
		//flag = false;
		//break;
	}

	/*
	//bool flag = true;
	int maxn = 0;
	for(int i=N+1;i>0;i--){
		if(humans[i] < nokemono[i]) nokemono[i] = humans[i];
		if(humans[i] < i){
			//cout << i << endl;
			nokemono[i] = 0;
		}
		if(nokemono[i] > maxn) maxn = nokemono[i];
		//if(nokemono[i] < humans[i]) nokemono[i] = 0;
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
		//cout << i << "," << humans[i]-1 << endl;
		//flag = false;
		//break;
	}
	*/
	

	cout << max(0,maxn-1) << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;

struct info {
	int min;
	int max;
	bool flg = true;
};

bool info_first(const info& a, const info& b) {
	return a.min > b.min;
}
bool info_second(const info& a, const info& b) {
	return a.max < b.max;
}

signed main() {
	cin1(n);

	vector<info> P(n);
	for (int i = 0; i < n; i++) {
		cin2(P[i].min,P[i].max);
	}
	
	sort(P.begin(),P.end(),info_first);

	int ans = n + 1;	
	for (int i = 0; i < n; i++) {
		//cout << P[i].min << " " << P[i].max << " " << endl;	
		if (P[i].min > ans) {
			ans--;
			P[i].flg = false;
		}
	}
	//cout(ans);
	
	sort(P.begin(),P.end(),info_second);
	for (int i = 0; i < n; i++) {
		//cout << P[i].min << " " << P[i].max << " " << endl;	
		if (P[i].flg and P[i].max < ans) {
			ans--;
		}
	}

	cout(ans-1);

	return 0;
}

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 <algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    int sum[100010]={};
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        sum[a]++;
        sum[b+1]--;
    }
    sum[0]++;
    sum[100002]--;
    for(int i=0;i<100003;i++){
        sum[i+1]+=sum[i];
    }
    int ans=0;
    for(int i=0;i<=n+1;i++){
        if(i<=sum[i])ans=i-1;
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

double dist(Point p) { return abs(p); }//ベクトルpの絶対値
double dist(Point a, Point b) {//2点間の距離
	return abs(a - b);
}

int n;
int sum[100100];

int main() {
	cin >> n;
	sum[0]++;
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	rep(i, 100100 - 1)sum[i + 1] += sum[i];
	ll ans = 0;
	rep(i, n) {
		ll num = i + 1;
		//cout << num << " " << sum[num] << endl;
		if (sum[num] >= num)ans = num;
	}
	cout << ans - 1 << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
using namespace std; 
const int INF = 1e9;

int main(){
    int n;
    while(cin >> n){
        int acc[100010] = {};
        rep(i,n){
            int a,b;
            cin >> a >> b;
            ++acc[a];
            --acc[b+1];
        }
        int x = 0;
        int ans = 0;
        rep(i,100005){
            x += acc[i];
            if(x == i) ans = max(ans, x-1);
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<queue>
#include<utility>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

int main(){
	int n;
	cin>>n;
	vector<pair<int,int> > v;
	for(int i=0;i<n;i++){
		pair<int,int> p;
		cin>>p.first>>p.second;
		v.push_back(p);
	}
	sort(v.begin(),v.end());
	priority_queue<int,vector<int>,greater<int> > pq;
	int ans=1,sz=0;
	for(int i=0;i<n;i++){
		int s=v[i].first;
		pq.push(v[i].second);
		sz++;
		while(pq.top()<s){
			pq.pop();
			sz--;
		}
		if(s<=sz+1)ans=max(ans,sz);	
	}
	cout<<ans-1<<endl;
	return 0;
}