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<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<stack>
#include<string>
#include<algorithm>
#include<functional>
#include<cstring>
#include<utility>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

/**** Type Define ****/

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> Q;

/**** Const List   ****/

const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, -1, 0, 1};

/**** General Functions ****/

ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}

ll extgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0; return a;
  }
  ll q = a/b, g = extgcd(b, a - q*b, x, y);
  ll z = x - q * y;
  x = y;
  y = z;
  return g;
}

ll invmod (ll a, ll m) { // a^-1 mod m
  ll x, y;
  extgcd(a, m, x, y);
  x %= m;
  if (x < 0) x += m;
  return x;
}

ll nCk(ll n, ll k, ll mod) {
  ll ans = 1;
  for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod;
  return ans;
}

ll lmin(ll a, ll b) { return a > b ? b : a; };

ll lmax(ll a, ll b) { return a > b ? a : b; };

ll lsum(ll a, ll b) { return a + b; };

/**** Zip ****/

template <typename T>
class Zip {
  vector<T> d;
  bool flag;
public:
  Zip() {
    flag = false;
  }
  void add(T x) {
    d.push_back(x);
    flag = true;
  }
  ll getNum(T x) { // T need to have operator < !!
    if (flag) {
      sort(d.begin(), d.end());
      d.erase(unique(d.begin(), d.end()), d.end());
      flag = false;
    }
    return lower_bound(d.begin(), d.end(), x) - d.begin();
  }
  ll size() {
    if (flag) {
      sort(d.begin(), d.end());
      d.erase(unique(d.begin(), d.end()), d.end());
      flag = false;
    }
    return (ll)d.size();
  }
};

/**** Union Find ****/

class UnionFind {
  vector<ll> par, rank; // par > 0: number, par < 0: -par
public:
  void init(ll n) {
    par.resize(n, 1); rank.resize(n, 0);
  }
  ll getSize(ll x) {
    return par[find(x)];
  }
  ll find(ll x) {
    if (par[x] > 0) return x;
    return -(par[x] = -find(-par[x]));
  }
  void merge(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[y] += par[x];
      par[x] = -y;
    } else {
      par[x] += par[y];
      par[y] = -x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool isSame(ll x, ll y) {
    return find(x) == find(y);
  }
};

/**** Segment Tree ****/

template <typename T>
class SegmentTree {
  ll n;
  vector<T> node;
  function<T(T, T)> fun, fun2;
  bool customChange;
  T outValue, initValue;
public:
  void init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) {
    // changeFunction: (input, beforevalue) => newvalue
    fun = resultFunction;
    fun2 = changeFunction;
    customChange = changeFunction != NULL;
    n = 1;
    while (n < num) n *= 2;
    node.resize(2 * n - 1, init);
    outValue = out;
    initValue = init;
  }
  void valueChange(ll num, T value) {
    num += n-1;
    if (customChange) node[num] = fun2(value, node[num]);
    else node[num] = value;
    while (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
  }
  T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
    if (r == -1) r = n;
    if (a <= l && r <= b) return node[k];
    if (b <= l || r <= a) return outValue;
    ll mid = (l + r) / 2;
    return fun(rangeQuery(a, b, l, mid, 2*k+1), rangeQuery(a, b, mid, r, 2*k+2));
  }
};

/**** LIS ****/
ll lis(ll* a, ll n, ll* dp) {
  fill(dp, dp + n, INF);//INFを代入
  for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i];
  return (ll)(lower_bound(dp, dp + n, INF) - dp);
}

ll binarySearch(function<bool(ll)> check, ll ok, ll ng) {
	while ((ok - ng > 1) || (ng - ok > 1)) {
		ll mid = (ok + ng) / 2;if (check(mid)) ok = mid;else ng = mid;
	}
	return ok;
}
bool check(ll input) {
// inputが可能ならtrue}// printf(“%lld\n”, binarySearch(check, 0, INF));
}

/**** main function ****/
ll n,x[100004],answ[100002],ans;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		ll a,b;
		cin>>a>>b;
		x[a-1]++;
		x[b]--;
	}
	for(int i=1;i<=100000;i++){
		answ[i]=answ[i-1]+x[i];
	}
	for(int i=0;i<=n;i++){
		if(i<answ[i]){
			answ[i]=i;
		}
		if(i>answ[i]){
			continue;
		}
		ans=lmax(ans,answ[i]);
	}
	cout<<ans<<endl;
}

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

#define MAX 100002

int main() {
  int n;
  int a, b;
  int v[MAX] = {};
  
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    v[a] ++;
    v[b+1] --;
  }

  int now = 0;
  int ans = 0;
  for(int i=0; i<MAX; i++) {
    now += v[i];
    if(i <= now+1) ans = i-1;
  }

  cout << ans << endl;
  
  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]--;
  }

  signed int sum = 0;
  for(int i = 0; i < 100001; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(){
  int num[100005]={0};
  int n,m=0;
  cin>>n;
  int a,b;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++){
      num[j]++;
    }
  }

  for(int i=0;i<10004;i++){
    num[i+1]+=num[i];
  }
  for(int i=10004;i>0;i--){
    if(num[i]>=i){
      cout<<i-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,m){
		co+=in[i];
		//cout<<i<<" "<<co<<endl;
		if(i-1<=co)ma=max(ma,i-1);
	}
	cout<<ma<<endl;
}