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 <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
int imos[100010];

signed main() {
    cin >> N;
    rep(i,0,N) {
        int a, b; cin >> a >> b;
        imos[a]++; imos[b+1]--;
    }

    rep(i,1,100010) imos[i] += imos[i-1];
    int ans = 0;
    rep(i,0,100010) {
        // if(i < 10) printf("i = %lld, imos = %lld\n", i, imos[i]);
        if(imos[i] + 1 >= i) chmax(ans, i-1);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main()
{
  int friends[100005]={};
  int i;
  int n;
  int a,b;
  int max=0;

  cin>>n;
  for(i=0;i<n;i++){
    cin>>a>>b;
    friends[a]++;
    friends[b+1]--;
    //    cout<<"i"<<i<<endl;
  }
  for(i=1;i<100000;i++)
    friends[i]+=friends[i-1];
  for(i=0;i<100000;i++)
    if(friends[i]>=i-1)
      max=i-1;

  cout<<max<<endl;
}

C++:  #include <iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define P pair<int,int>
#define mp make_pair


int t[100002];
int x[100002];
int y[100002];
int z[100002];
int n;
int main(){
	cin>>n;
	//for(int i=0;i<=n;i++)t[i]=1;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
			for(int j=a-1;j<b;j++)t[j]++;
			y[a]++;
			z[b]--;
	}
	
	int ans=0;
	//for(int i=0;i<=n;i++)cout<<t[i]<<" ";cout<<endl;
	for(int i=0;i<=n+1;i++)if(t[i]==i+1)ans=t[i];//t[i]??\??????????????°i???????????°
	if(ans!=0)ans--;
	cout<<ans<<endl;
	return 0;

}

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

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

const int MAX = 100000;

int dp[MAX + 1] = {};

int main() {
	int n; cin >> n;
	
	int a, b, res = 0;
	REP( i, n ) {
		cin >> a >> b;
		FOR( i, a, b + 1 ) {
			dp[i]++;
			if( dp[i] == i - 1 ) res = max( dp[i], res );
		}
	}

	cout << res << endl;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;

int n;
int syaku[100010];
int main(int argc,char const* argv[])
{
  cin >> n;
  memset(syaku,0,sizeof(syaku));
  for(int i=0;i<n;i++)
    {
      int l,r;
      cin >> l >> r;
      syaku[l]++;
      syaku[r+1]--;
    }
  for(int i=1;i<100010;i++)
    {
      syaku[i]+=syaku[i-1];
    }
  int ans=0;
  for(int i=0;i<=n+1;i++)
    {
      if((i-1)<=syaku[i])
	{
	  ans=i-1;
	}
    }
  cout << ans << 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>

using namespace std;

int r[100100];
int main(){

    int N;//<=10^5
    cin>> N;
    int a[N], b[N];
    for(int i=0; i<N; i++) cin>> a[i]>> b[i];

    for(int i=0; i<N; i++){
        r[a[i]]++;
        r[b[i]+1]--;
    }
    for(int i=0; i+1<100100; i++){
        r[i+1]+=r[i];
    }
    int s=0;
    for(int i=0; i<100100; i++){
        if(r[i]>=i) s=max(s, i-1);
    }

    cout<< s<< endl;

    return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int N, z[200000];

int main() {
    scanf("%d", &N);
    rep (_, N) {
        int a, b;
        scanf("%d%d", &a, &b);
        z[a]++;
        z[b+1]--;
    }
    int ans = 1, cur = 1;
    rep (i, N+2) {
        cur += z[i];
        if (cur >= i) ans = i;
    }
    printf("%d\n", ans-1);
    return 0;
}

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

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

        for(j = a; j <= b; j++){
            array[j]++;
        }
    }
    
    for(i = 2; i < 100002; i++){
        if(i-1 <= array[i]){
            ans = i-1;
        }
    }
    cout << ans << endl;
    
    return 0;
    
}