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 <queue>
#include <algorithm>
#include <map>

using namespace std;

const int N = 100001;

int cnt[2][N+1];

main(){
  int n;
  cin >> n;
  fill(cnt[0], cnt[2], 0);
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    cnt[0][a]++;
    cnt[1][b]++;
  }
  int ans = 0;
  int sum = 1;
  for(int i=0;i<N+1;i++){
    sum += cnt[0][i];
    sum -= cnt[1][i];
    if(sum >= i) ans = max(ans, i - 1);
  }
  cout << ans << endl;
}

C++: #include<iostream>
#include<array>

using namespace std;

int main()
{
	array<int, 100000> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	//cout << 1 << endl;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		if(friends[i] > ans && friends[i] - i >= 0){
			ans = max(ans, friends[i]);
			if(ans - 1 == i)ans++;
		}
		//cout << friends[i] << endl;
	}

	//cout << ans << endl;
	ans = max(0, ans - 2);
	cout << ans << endl;
	return 0;
}

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

int dp[100002];

int main() {

    int n;

    cin >> n;

    int a,b;

    for(int i=0;i<n;i++){
        cin >> a >> b;
        a--;b--;
        for(int j=a;j<=b;j++){
            dp[j]++;
        }
    }

    int ans = 0;

    for(int i=2;i<=100001;i++){
        if(dp[i]>=i-1) {
            ans = i-1;
        }
    }

    cout << ans << endl;

    return 0;
}

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

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

	int ans=0,tmp=0;
	for(int i=2; i<N+2; i++)
	{
		tmp+=x[i];
		ans=max(ans,tmp);
	}

	cout << ans-1 << endl;
}

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

int data[100005];

int main(){
	int n;
	cin >> n;

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[j]++;
	}

	int ans = 0;
	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = data[i];
		if(ok + 1 >= i) ans = max(ans, i);
	}

	cout << ans - 1 << endl;

	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(1000010);
  ans[0] = 0;
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    --a;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,100001){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,0,n+1){
    if(ans[i] >= i)
      k = max(k,i+ 0LL); 
  }
  cout << k << endl;
  return 0;
}

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

using namespace std;

int n;
int cnt[100005];

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

    int ans = 0;
    for(int i = 1; i < n + 2; i++){
        if(i <= cnt[i] + 1) ans = cnt[i] - 1;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int Digit[100002];
	for(int i = 0; i < 100002; ++i)
	{
		Digit[i] = 0;
	}
	int N;
	cin >> N;
	vector<P> Person(N);
	for(int i = 0; i < N; ++i)
	{
		cin >> Person[i].first >> Person[i].second;
	}
	for(int i = 0; i < N; ++i)
	{
		for(int j = Person[i].first; j <= Person[i].second; ++j)
		{
			++Digit[j];
		}
	}
	int Max = 0;
	for(int i = 2; i <= N; ++i)
	{
		if(Max < Digit[i])
		{
			Max = Digit[i];
		}
	}
	cout << Max - 1 << endl;
}

int main()
{
	solve();
	return(0);
}

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<100001;i++){
    //if(i<10)
    //cout << i <<":" << ma[i]<<endl;
    if(i<=ma[i])
      m=max(m,i-1);
  }
  cout << m << endl;
  return 0;
}

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

bool comp(const pair<string,int> a, const pair<string,int> b) {
  if (a.first != b.first)
    return a.first < b.first; // 高い順に並べたいので演算子を逆に
  else{
      return a.second > b.second;
  }
}

//priority_queue<long long int,vector<long long int>, greater<long long int>> PQ;
//priority_queue<long long int> PQ1;




int main(){
    int list[100003] = {0};
    long long int Rsum[100003] = {0};

    int N;

    cin >>N;

    for(int i = 0;i<N;i++){
        int a,b;

        cin >>a>>b;

        list[a] += 1;
        list[b+1] += -1;
    }

    for(int i = 1;i <= 100003;i++){
        Rsum[i] = Rsum[i-1] + list[i];
    }

    long long int ans = -1;

    for(int i = 0;i <= N+1;i++){
       if(Rsum[i]+1 >= i){
           ans = i;
       }
    }

    cout <<ans-1<<endl;



  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    int come[120000] = {0};
    come[0]++;
    for (int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        come[a]++;
        come[b + 1]--;
    }
    for (int i = 1; i < 120000; i++)
        come[i] += come[i - 1];
    int res = 1;
    for (int i = 2; i < 120000; i++)
    {
        if (i <= come[i])
            res = i;
    }
    cout << res - 1 << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
    int N; cin >> N;
    vector<int> num(N + 2);
    rep(i, N) {
        int a, b; cin >> a >> b;
        if (a > N + 1) continue;
        num[a - 1]++; num[min(b, N + 1)]--;
    }
    int maxi = 0;
    for (int i = 1; i < N + 2; ++i) {
        num[i] += num[i - 1];
        if (num[i] >= i) maxi = i;
    }
    cout << maxi << endl;
}