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>
#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++) {
		if (P[i].min > ans) {
			ans--;
			P[i].flg = false;
		}
	}
	
	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 <bits/stdc++.h>

using namespace std;

int main(){
  int N; cin >> N;
  const int MAX = 1e5+10;
  vector<int> cnt(MAX);
  for(int i=0;i<N;++i){
	int b, e; cin >> b >> e;
	++e;
	cnt[b]++;
	cnt[e]--;
  }

  vector<int> acc(MAX);
  for(int i=0,s=0;i<MAX;++i)
	acc[i] = (s += cnt[i]);

  int ans = 0;
  for(int i=0;i<MAX;++i)
	if(acc[i] >= i-1)
	  ans = i-1;
  
  cout << ans << endl;
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n,a[100005];

int main(){
  cin>>n;
  for(int i=0,s,t;i<n;i++)cin>>s>>t,a[s-1]++,a[t-1]--,a[s-1]+=(s==t);
  int ans=0;
  for(int i=1;i<=n;i++){
    a[i]+=a[i-1];
    if(i<=a[i]) ans=max(i,ans);
  }
  cout <<ans<<endl;
  return 0;
}

C++: #include <set>
#include <cstdio>
#include <cctype>
#include <unistd.h>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

char buffer[4 * 1024 * 1024];
int pos = 0;
int end = 0;

__attribute__((always_inline)) int mygetchar(){
  if(pos == end){
    end = read(0, buffer, sizeof(buffer));
    pos = 0;
  }
  return buffer[pos++];
}

__attribute__((always_inline)) int getInt(){
  int ret = 0, c;
  c = mygetchar();
  while(!isdigit(c)) c = mygetchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = mygetchar();
  }
  return ret;
}

int a[100000 + 3];
int b[100000 + 3];

int main(){
  int n = getInt();

  REP(i,n){
    int aa = getInt();
    int bb = getInt();

    a[aa]++; b[bb]++;
  }

  int ans = 0;
  int peo = 1;

  REP(i,100000 + 3){
    peo += a[i];
    if(i > 0) peo -= b[i - 1];

    if(peo >= i){
      ans = i - 1;
    }
  }

  printf("%d\n", ans);

  return 0;
}

C++: #include <set>
#include <cstdio>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int a[100000 + 1];
int b[100000 + 1];

int main(){
  int n = getInt();

  REP(i,n){
    int aa = getInt();
    int bb = getInt();

    a[aa]++; b[bb]++;
  }

  int ans = 0;
  int peo = 1;

  REP(i,100000 + 1){
    peo += a[i];
    if(i > 0) peo -= b[i - 1];

    if(peo >= i){
      ans = i - 1;
    }
  }

  printf("%d\n", ans);

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
           
#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
    int n;
    static int new_ok[100010];
    scanf("%d", &n);
    rep (i, n)
    {
	int a, b;
	scanf("%d%d", &a, &b);
	++new_ok[min(b, n + 1)];
	--new_ok[min(a - 1, n + 1)];
    }
    ++new_ok[n + 1];

    int res = -3, ok = 0;
    for (int i = n + 1; i > 0; --i)
    {
	ok += new_ok[i];
	if (ok >= i)
	{
	    res = i - 1;
	    break;
	}
    }
    printf("%d\n", res);
}

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


int main(){
    int N; cin >> N;
    int imos[100010],check[100010];
    fill(check,check+100001,0);
    int a,b;
    for(int i = 0; i < N; i++){
        cin >> a >> b;
        a--; 
        check[a]++;
        check[b]--;
    }
    imos[0] = check[0];
    for(int i = 1; i <= 100001; i++){
        imos[i] = imos[i-1]+check[i];
    }
    int ans = 0;
    for(int i = 0; i <= 100001; i++){
        if(imos[i] >= i){
            ans = max(ans,i);
        }
    }
    cout << ans << endl;
}

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

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