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++: using namespace std;
#include <vector>
#include <utility>
#include <functional>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

template < typename T >
using fun = function<T>;

template <typename T> class Stream {
  private:
    vector<T> value;

    template <typename F, typename U, typename B, typename E>
    auto fold(B iter, E end, F f, U u) -> decltype(f(u, *iter)) {
      if (iter != end) {
        u = f(u, *iter);
        iter++;
        for (; iter != end; iter++)
          u = f(u, *iter);
      }
      return u;
    }

    template <typename F, typename U, typename B, typename E>
    auto scan(B iter, E end, F f, U u) -> Stream<decltype(f(u, *iter))> {
      vector<T> v;
      v.push_back(u);
      if (iter != end) {
        u = f(u, *iter);
        v.push_back(u);
        iter++;
        for (; iter != end; iter++) {
          u = f(u, *iter);
          v.push_back(u);
        }
      }
      Stream<decltype(f(u, *iter))> s(v);
      return s;
    }

  public:
    Stream(initializer_list<T> &&v) {value = v;};
    Stream(vector<T> v) : value(v) {};
    Stream(int n) {value.resize(n);};
    Stream() {};
    
    Stream& operator= (initializer_list<T> &&v) {value = v;};
    T& operator[] (int n) {return value[n];};

    void push_back(T t) {
        value.push_back(t);
    }   

    size_t size() {
        return value.size();
    }   

    void reserve(size_t s) {
        value.reserve(s);
    }   

    void resize(size_t s) {
        value.resize(s);
    }

    vector<T> unwrap() {
      return value;
    }

    Stream<T> reverse() {
      vector<T> v = value;
      std::reverse(v.begin(), v.end());
      return Stream<T>(v);
    }

    template <typename U>
    Stream<pair<T, U>> zip(Stream<U> s) {
      Stream<pair<T, U>> z;
      z.reserve(min(value.size(), s.size()));
      for (size_t i=0; i<min(value.size(), s.size()); i++)
        z.push_back(make_pair(value[i], s[i]));
      return z;
    }

    template <typename F>
    auto map(F f) -> Stream<decltype(f(value[0]))> {
      vector<decltype(f(value[0]))> c;
      c.reserve(value.size());
      for (auto x: value) c.push_back(f(x));
      return Stream<decltype(f(value[0]))>(move(c));
    }

    template <typename F>
    auto foreach(F f) -> decltype(f(value[0])) {
      for (auto x: value) f(x);
    }

    Stream<T> filter(fun<bool(T)> f) {
      vector<T> c;
      for (auto x: value) if (f(x))
        c.push_back(x);
      return Stream<T>(move(c));
    }

    template <typename F, typename U>
    auto foldr(F f, U u) -> decltype(f(u, value[0])) {
      return fold(value.rbegin(), value.rend(), f, u);
    }

    template <typename F, typename U>
    auto foldl(F f, U u) -> decltype(f(u, value[0])) {
      return fold(value.begin(), value.end(), f, u);
    }

    template <typename F, typename U>
    auto scanr(F f, U u) -> Stream<decltype(f(u, value[0]))> {
      return scan(value.rbegin(), value.rend(), f, u);
    }

    template <typename F, typename U>
    auto scanl(F f, U u) -> Stream<decltype(f(u, value[0]))> {
      return scan(value.begin(), value.end(), f, u);
    }

    Stream<T> take(size_t n) {
      vector<T> v;
      for (size_t i=0; i<value.size() && i<n; i++)
        v.push_back(value[i]);
      return Stream<T>(v);
    }

    Stream<T> drop(size_t n) {
      vector<T> v;
      for (size_t i=n; i<value.size(); i++)
        v.push_back(value[i]);
      return Stream<T>(v);
    }

    void debug() {
      for (auto x: value) 
        cout << x << " ";
      cout << endl;
    }

    auto begin() -> decltype(value.begin()) { 
      return value.begin(); 
    }

    auto end() -> decltype(value.end()) { 
      return value.end(); 
    }
};

int main(){
  int n;
  cin >> n;
  vector<int> imos(100001+10, 0);
  for (int i=0; i<n; i++) {
    int n, m; cin >> n >> m;
    imos[n]++; imos[m+1]--;
  }
  vector<int> z(imos.size());
  for (int i=0; i<z.size(); i++) z[i] = i-1;

  auto xs = Stream<int>(imos)
    .scanl([](int x, int y){return x + y;}, 0) 
    .drop(1)
    .zip(Stream<int>(z))
    .map([](auto p){return p.first>p.second ? min(p.first, p.second) : 0;})
    .unwrap();
  cout << *max_element(xs.begin(), xs.end()) << endl;
  return 0;
}

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

int main(){
    int n;  cin>>n;
    int OK[n][2];   for(int i=0;i<n;i++) cin>>OK[i][0]>>OK[i][1];
    
    int result[n+2];  for(int i=0;i<n+2;i++)result[i]=0;

    for(int i=1;i<=n+1;i++){ // i は（私を含め）行く人数
        for(int j=0;j<n;j++){
            if(OK[j][0]<=i && i<=OK[j][1])result[i]++;
        }

        if(i<=result[i])result[i]=i-1;
        else if(i-1>result[i])result[i]=-1;
    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

//    for(int i=0;i<n+2;i++){
//        if(i<=result[i])result[i]=i-1;
//        else if(i-1>result[i])result[i]=-1;
//    }

//    for(int i=0;i<n+2;i++){
//        cout<<result[i]<<":";
//    }cout<<endl;

    int max=0;
    for(int i=0;i<n+2;i++){
        if(result[i]>max)max=result[i];
    }

    cout<<max<<endl;


    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
int a[100005];
int b[100005];
int imos[100005];
bool ok(int m) { return imos[m] >= m - 1; }
int main() {
    cin >> N;
    rep(i, N) cin >> a[i] >> b[i];
    rep(i, N) {
        ++imos[a[i]];
        --imos[b[i] + 1];
    }
    rep(i, 100005) if (i) imos[i] += imos[i - 1];
    // rep(i, N + 1) cout << i << ", " << imos[i] << endl;
    int ans = 0;
    rep(i, N + 1) if (ok(i)) ans = i;
    cout << ans - 1 << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i, a, n) for(int i=a; i<n; i++)

int main()
{
	int N;
	vector<int> member(100002, 0);
	cin >> N;
	REP(i, 0, N) {
		int a, b;
		cin >> a >> b;
		member[a-1]++;
		member[b]--;
	}
	REP(i, 0, 100001) member[i + 1] += member[i];

	int ans = 0;
	REP(i, 0, 100002) {
		if (member[i] >= i) ans = max(ans, i);
	}

	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
#define MAX 100001
int x[MAX];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 0; i < MAX; i++) x[i]=0;
  int N; cin >> N; N++;
  for (int i = 0; i < N-1; i++) {
    int l,r; cin>>l>>r;
    if (l > N) continue;
    r=min(r,N);
    for (int j = l; j <= r; j++) {
      x[j]++;
    }
  }

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