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++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const static int tx[] = {0,1,1,1,0,-1,-1,-1};
const static int ty[] = {-1,-1,0,1,1,1,0,-1};
 
static const double EPS = 1e-8;

class Condition{
public:
  int lower;
  int upper;
  Condition(int _l,int _u) : lower(_l), upper(_u) {}
  bool operator<(const Condition& c) const{
    if(lower != c.lower){
      return lower < c.lower;
    }else{
      return upper < c.upper;
    }
  }
  bool operator>(const Condition& c) const{
    if(lower != c.lower){
      return lower > c.lower;
    }else{
      return upper > c.upper;
    }
  }
};

int main(){
  int N;
  while(~scanf("%d",&N)){
    priority_queue<Condition,vector<Condition>,greater<Condition> > ppl;
    for(int i=0;i<N;i++){
      int lower,upper;
      scanf("%d %d",&lower,&upper);
      ppl.push(Condition(lower,upper));
    }
    int current = 1;

    int res = 0;
    int dp[100100];
    memset(dp,0,sizeof(dp));

    while(!ppl.empty()){
      Condition c = ppl.top();
      ppl.pop();
      if(current + 1 >= c.lower){
        current++;
        current -= dp[current];
        dp[c.upper + 1]++;
        res = max(current - 1,res);
      }
    }

    printf("%d\n",res);
  }
}

C++: #include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define rep(i, j, k) for (auto i = j; i < k; i++)
#define rrep(i, j, k) for (auto i = j; i > k; i--)
using namespace std;

int sum[100011];

int main() {
  int n; cin >> n;
  rep(i, 0, n) {
    int a, b; cin >> a >> b;
    sum[a]++;
    sum[b + 1]--;
  }
  rep(i, 1, 100011) sum[i] = sum[i] + sum[i - 1];
  int _max = 0;
  rep(i, 0, 100011) {
    if (sum[i] >= i - 1) _max = max(_max, i - 1);
  }
  cout << _max << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<functional>
using namespace std;

#define INF 1<<21
#define MOD 1000000007

int main(void) {
  int n;
  int a, b;
  cin >> n;
  int ans[100005];
  fill(ans, ans + 100005, 0);
  for (int i = 0; i < n; i++) {
	cin >> a >> b;
	ans[a]++;
	ans[b + 1]--;
  }

  int m = 0;
  for (int i = 0; i < n; i++) {
	ans[i + 1] += ans[i];
	m = max(m, ans[i + 1]);
  }
  cout << m-1 << endl;

}

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;
      c[a]++;
      c[b+1]--;
  }
  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>
using namespace std;

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

int main() {
    Int n;
    cin >> n;
    vector<Int> a(100005, 0);
    for (Int i = 0; i < n; i++) {
        Int x, y;
        cin >> x >> y;
        x--;
        y--;
        a[x]++;
        a[y + 1]--;
    }
    for (Int i = 0; i <= 100000; i++) {
        a[i + 1] += a[i];
    }
    Int res = 0;
    for (Int i = 0; i <= 100000; i++) {
        if (a[i] >= i) {
            res = i;
        }
    }
    dump(res);
    return 0;
}

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

#define MAX 100002

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

  int now = 0;
  int ans = 0;
  for(int i=0; i<MAX; i++) {
    now += v[i];
    if(i <= now+1) ans = i-1;
  }

  cout << ans << endl;
  
  return 0;
}

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

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void)
{
    ll n; cin>>n;
    vector<ll> s(1e6,0);
    rep(i,n){
        ll a,b; cin>>a>>b;
        s[--a]++;
        s[b]--;
    }
    ll ans=0;
    for(ll i=1;i<=n;++i){
        s[i]+=s[i-1];
        if(i<=s[i]) ans=i;
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

#define MAX 1e05+1

int main(void) {
    int n;
    cin >> n;
    int start;
    int end;
    VI table(MAX + 1, 0);

    for (int i = 0; i < n; i++) {
        cin >> start >> end;
        start--;
        end--;
        table[start]++;
        table[end + 1]--;
    }

    for (int i = 1; i <= MAX; i++) {
        table[i] += table[i - 1];
    }

    for (int i = MAX; i >= 0; i--) {
        if (i <= table[i]) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

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++){
		int a,b;
		cin>>a>>b;
			for(int j=a-1;j<b;j++)t[j]++;
			y[a]++;
			z[b]--;
	}
	
	int ans=0;
	z[0]+=n;
	for(int i=1;i<=n+1;i++){
	y[i]+=y[i-1];
	z[i]+=z[i-1];
	}
	//for(int i=1;i<=n+1;i++)cout<<y[i]<<" "<<z[i]<<endl;
	for(int i=1;i<=n+1;i++)if(y[i]>=z[i])ans=max(ans,min(y[i],z[i]));

	cout<<ans<<endl;
	return 0;

}