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(1, *max_element(xs.begin(), xs.end())) << endl;
  return 0;
}

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

int main(){
  int N, a, b, temp[100101] = {}, ma = 0;

  cin>>N;

  for(int i = 0; i < N; i++){
    cin>>a>>b;
    temp[a]++;
    temp[b+1]--;
  }

  temp[0] = 1;

  for(int i = 1; i <= 100101; i++){
    temp[i]+= temp[i-1];
    if(temp[i] >= i){
      ma = i;
    }
    // cout<<temp[i]<<" ";
  }

  cout<<ma-1<<endl;
  
  return 0;
}