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>
using namespace std;
int main(){
  int ma[100005]={};
  int n;
  cin >> n;
  int i,j,k;
  int a,b;
  for(i=0;i<n;i++){
    cin >> a >> b;
    for(j=a;j<=b;j++) ma[j]++;
  }
  int m=0;
  for(i=0;i<100005;i++){
    //if(i<10)
    //cout << i <<":" << ma[i]<<endl;
    if(i<=ma[i]+1)
      m=max(m,i-1);
  }
  cout << m << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    if( a == b) d[a]++;
    else{
      c[a]++;
      c[b]--;
    }
  }
for(int i = 0;i<c.size();i++){
  c[i] += d[i];
}
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 100002
using namespace std;
int n,a,b,d[N],ans;

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

C++: #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
vector<int> in_(100010,0);
vector<int> out_(100010,0);
int main(){
    cout << fixed << setprecision(10);
    int n;
    cin >> n;
    rep(i,n) {
        int x,y;
        cin >> x >> y;
        in_[x]++;
        out_[y]++;
    }
    int ans = 0;
    int now = 0;
    rep(i,100010){
        now += in_[i];
        if(now + 1 >= i) chmax(ans,i-1);
        now -= out_[i];
    }
    cout << max(ans, 0)<< endl;
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int solve(){
    int res = 0;
    int N; cin >> N;
    vector<int> imos(100010,0);
    for(int i = 0; i < N;i++){
        int a,b; cin >> a >> b;
        imos[a]++; imos[b+1]--;
    }
    for(int i = 1; i <= N;i++){
        imos[i] += imos[i-1];
        if(imos[i] >= i-1) res = i-1;
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    if( a == b) d[a]++;
    else{
      c[a]++;
      c[b]--;
    }
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  for(int i = 0;i<c.size();i++){
    c[i] += d[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i <= c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}

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

int main(){
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d %d",&a,&b);
    t[a-1]++;
    t[b]--;
  }
  int ans=0;
  for(int i=1;i<=100000;i++){
    t[i]+=t[i-1];
    if(i<=t[i])ans=i;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
using namespace std;
int main(){
    int n;
    int cnt[100100];
    memset(cnt,0,sizeof(cnt));
    cin>>n;
    REP(i,n){
        int a,b;
        cin>>a>>b;
        cnt[a]++;cnt[b+1]--;
    }
    int ans=0;
    REP(i,100000)cnt[i+1]+=cnt[i];
    REP(i,n+1){
        if(i==0)continue;
        if(cnt[i]>=i){
            ans=i-1;
        }
    }
    cout<<ans<<endl;
}

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

int main()
{
  int N;
  cin >> N;
  static int v[100010];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    ++v[a];
    --v[b+1];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 1; i <= N; i++) {
    acc += v[i];
    if (acc >= i) {
      ans = i-1;
    }
  }
  cout << ans << endl;
  return 0;
}

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // initialized by 0 // use key 100001

int n;
int min_;
int max_;

int solve() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		for (int j = min_; j < max_; j++) {
			box[j]++;
		}
	}
	for (int i = n + 1; i >= 0; i--) {
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N; cin >> N;
  int imos[100005]; CLR(imos);
  FOR(i,0,N) {
    int a, b; cin >> a >> b;
    imos[a]++;
    imos[b+1]--;
  }
  int ans = 0;
  FOR(i,0,N+1) {
    imos[i+1] += imos[i];
    if(i >= 1 && imos[i] >= i - 1) {
      ans = max(ans, i - 1);
    }
  }
  cout << ans << endl;
  return 0;
}