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>

struct Range {
	int beg;
	int end;
}; 

int main(void){
	int n;
	std::cin >> n;

	Range* ps = new Range[n];

	for(int i=0; i<n; i++) std::cin >> ps[i].beg >> ps[i].end;

//	i???"?????????"??????????????°
	int mps = 1;
	for(int i=1; i<=n+1; i++){
		int cps = 0;
		bool f = false;
		for(int j=0; j<n; j++){
			if(i >= ps[j].beg && i <= ps[j].end){
				cps ++;
				if(cps >= i-1){
					f = true;
					break;
				}
			}
		}
		if(f) mps = i;
	}

	std::cout << (mps-1) << '\n';

	system("pause");
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <cstdio>
#include <climits>
#include <complex>
#include <cstdint>
#include <tuple>

#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) {}
};

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<Edge>> AdjList;
AdjList graph;

const int INF = 100000000;


// 1-indexed

bool cmp(const pii& a, const pii&b){
    if(a.first == b.first){
        return a.second < b.second;
    }

    return a.first < b.first;
}
int a, b, c, d, e,f;
bool used[500010];

int main() {
    //cout << fixed << setprecision(15);

    int n; cin >> n;
    vi v(100010);

    REP(i, n){
        int a, b; cin >> a >> b;
        v[a]++;
        v[b+1]--;
    }

    for(int i=0; i<100010; ++i){
        if(i>0) v[i] += v[i-1];
    }

    int ans = 0;
    for(int i=1; i<100010; ++i){
        if(v[i]>=i-1){
            ans = max(ans ,i);
        }
    }
    cout << ans-1 << endl;
    
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n;
	cin>>n;
	int m=100010;
	vi in(m,0);
	rep(i,n){
		int a,b;
		cin>>a>>b;
		in[a]++;
		in[b+1]--;
	}
	int ma=-1;
	int co=0;
	rep(i,10){
		co+=in[i];
		//cout<<i<<" "<<co<<endl;
		if(i-1<=co)ma=max(ma,i-1);
	}
	cout<<ma<<endl;
}

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

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int Digit[100002];
	for(int i = 0; i < 100002; ++i)
	{
		Digit[i] = 0;
	}
	int N;
	cin >> N;
	vector<P> Person(N);
	for(int i = 0; i < N; ++i)
	{
		cin >> Person[i].first >> Person[i].second;
	}
	for(int i = 0; i < N; ++i)
	{
		for(int j = Person[i].first; j <= Person[i].second; ++j)
		{
			++Digit[j];
		}
	}
	int Max = 0;
	for(int i = 0; i <= N + 1; ++i)
	{
		if(Digit[i] + 1 >= i)
		{
			Max = i - 1;
		}
	}
	cout << Max << endl;
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const int N = 100001;

int main() {
    int n;
    cin >> n;
    VI s(N+2);
    s[1] = 1;
    REP(i,n){
        int a, b;
        scanf("%d %d", &a, &b);
        s[a]++;
        s[b+1]--;
    }
    FOR(i,1,N) s[i] += s[i-1];
    int ans = 0;
    REP(i,N+1) if (s[i] >= i) ans = i;

    cout << ans-1 << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
int N,answer;
int table[100000+10],ans[100000+10];
signed main(){
  cin >> N;
    for(int i=0;i<N;i++){
      int a,b;
      cin >> a >> b;
      a--,b--;
      table[a]++;
      table[b+1]--;
    }
    for(int i=1;i<=N;i++){
      ans[i]+=ans[i-1]+table[i];
      //cout << ans[i] << endl;
      if(i<=ans[i]) answer=i;
    }
    cout << answer << endl;
  return 0;
}