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 <vector>

using namespace std;
	

int main(){
	int N;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;
	}
	/*
	for(int range:nokemono){
		cout << range << endl;
	}
	*/
	
	bool flag = true;
	for(int i=N+1;i>0;i--){
		if(humans[i] < i) continue;
		if(nokemono[i] < humans[i]) continue;
		cout << i << endl;
		flag = false;
		break;
	}
	
	if(flag)
		cout << 0 << endl;
	return 0;
}

C++: /*
 * 2331.cc: A Way to Invite Friends
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

/* global variables */

int cs[MAX_N + 2];

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++) {
    int ai, bi;
    cin >> ai >> bi;
    cs[ai - 1]++;
    cs[bi]--;
  }

  for (int i = 0; i < n; i++) cs[i + 1] += cs[i];

  int maxc = 0;
  for (int i = n; i >= 1; i--)
    if (cs[i] >= i) {
      maxc = i;
      break;
    }

  printf("%d\n", maxc);
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
static const int MAX_N = 100000;

int N;
int memo[MAX_N + 3];

int main(){
	scanf("%d", &N);
	for(int i = 0; i < N; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		memo[a]++; memo[b + 1]--;
	}
	int res = 0;
	for(int i = 1; i <= N + 1; i++){
		memo[i] += memo[i - 1];
		if(memo[i] + 1 < i) continue;
		res = max(res, min(memo[i], i - 1));
	}
	printf("%d\n", res);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100001];
  lp(i,100001){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+1){
    if(ans<a[i]&&a[i]>=i) ans=a[i];
  }
  cout<<ans-1<<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+1;i>=2;i--)
    if(d[i]>=i-1)ans=max(ans,i);
  cout<<ans-1<<endl;
  return 0;
}

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

const int MAXN = 100005;
int N;
int a[MAXN], b[MAXN];

int main() {
  while (cin >> N) {
    map<int, int> m;
    for (int i = 0; i < N; ++i) {
      cin >> a[i] >> b[i];
      m[a[i]]++;
      m[b[i]]--;
    }
    int res = 1, num = 1, prev = 0;
    for (map<int, int>::iterator it = m.begin();
         it != m.end(); ++it) {
      if (num >= prev) {
        res = max(res, min(num, it->first - 1));
      }
      prev = it->first;
      num += it->second;
    }
    cout << res - 1 << endl;
  }
  return 0;
}

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

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

//////
using pii = pair<int,int>;

bool isok(vector<pii> &ab, int n){
  int acc = 0;
  for(auto&& x:ab){
    if(x.first <= n && n <= x.second)acc++;
  }
  return acc>=n-1;
}

int main(){
  int n;
  cin >> n;
  vector<pii> ab(n);
  for(auto&& x:ab)cin >> x.first >> x.second;
  int res = 0;bool f = false;
  for(int i=1;i<=n;i++){
    if(isok(ab,i+1)){
      f = true;
      res = i;
    }
  }
  cout << res << endl;
}

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

#define REP(i,a) for(long i=0;i<(a);++i)

typedef std::vector<int>  VI;

int N;
VI imos;
int main() {
    std::cin >> N;
    imos.resize(N+2, 0);

    REP(i, N) {
        int a, b;
        std::cin >> a >> b;
        ++imos[a];
        --imos[b];
    }

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    
    std::cout << ans << std::endl;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef pair<int, int> P;

long long n, a[100010], b[100010], ma=0, mi=100010;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] > ma)ma = a[i];
		if (b[i] < mi)mi = b[i];
	}
	if (mi - ma<0 || ma>n + 1)cout << 0 << endl;
	else if(n+1<mi){
		cout << n+1 << endl;
	}
	else {
		cout << mi - 1 << endl;
	}
	return 0;
}

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

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

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(syaku[i]>ans)
	{
	  ans=syaku[i]-1;
	}
    }
  cout << ans << endl;
  return 0;
}

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

int data[100001][100002];

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[i][j] = 1;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = 0;
		for(int j = 1; j <= n; j++){
			if(data[j][i]) ok++;
		}
		if(ok + 1 >= i) v.push_back(i);
	}

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}