Problem:
Problem B: 友だちの誘い方
明日から、待ちに待った夏休みが始まります。なのでわたしは、友だちを誘って、海に遊びに行くこ
とに決めました。
けれども、わたしの友だちには、恥ずかしがりやさんが多いです。その人たちは、あまり多くの人が
いっしょに来ると知ったら、きっと嫌がるでしょう。
ほかにも、わたしの友だちには、目立ちたがりやさんも多いです。その人たちは、いっしょに来る人
があまり多くないと知れば、きっと嫌がるでしょう。
それと、わたしの友だちには、いつもは目立ちたがりやなのに実は恥ずかしがりやさんな人もいま
す。その人たちは、いっしょに来る人が多すぎても少なすぎても、きっと嫌がるでしょう。
こういうのは、大勢で行った方が楽しいはずです。だからわたしは、できるだけたくさんの友だちを
誘いたいと思っています。けれども、嫌がる友だちを無理やり連れていくのはよくありません。
いったい、わたしは最大で何人の友だちを誘うことができるのでしょうか。
わたしは、こういう頭を使いそうな問題が大の苦手です。なので、あなたにお願いがあります。もし
よろしければ、わたしの代わりにこの問題を解いていただけないでしょうか。いえ、決して無理にと
は言いません。けれど、もし解いていただるのでしたら、わたしはとても嬉しいです。
Input
N
a
1
b
1
a
2
b
2
.
.
.
a
N
b
N
入力の１行目には、整数N（1 ≤
N
≤ 100,000）が書かれている。これは、友だちの数をあらわす。
続く
N
行には、整数
a
i
と整数
b
i
（2 ≤
a
i
≤
b
i
≤ 100,001）が、空白区切りで書かれている。１＋ i 行目に書かれた整数
a
i
と
b
i
は、i 番目の友だちは海に行く人数が
a
i
人以上
b
i
人以下でないと嫌がることをあらわす。海に行く人数には「わたし」も含まれることに注意せよ。
Output
嫌がる友だちが出ないように、海に誘うことのできる友だちの最大人数を出力せよ。
Sample Input 1
4
2 5
4 7
2 4
3 6
Sample output 1
3
Sample Input 2
5
8 100001
7 100001
12 100001
8 100001
3 100001
Sample output 2
0
Sample Input 3
6
2 9
4 8
6 7
6 6
5 7
2 100001
Sample output 3
5

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;cin >> n;
  vi rui(100100);
  rui[0]++;
  rep(i,0,n){
    int a,b;cin >> a >> b;
    rui[a]++;rui[b+1]--;
  }
  rep(i,0,100010)rui[i+1] += rui[i];
  rrep(i,0,100100){
    if(rui[i] >= i){
      cout << i-1 << endl;
      break;
    }
  }
  

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3852
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAX = 1e5;

int main()
{
	ll N;
	cin >> N;
	vector<ll> range(MAX + 3);
	for (int i = 0, a, b; i < N; i++) {
		cin >> a >> b;
		range[a]++;
		range[b + 1]--;
	}
	ll res = 0;
	for (ll i = 1; i <= MAX + 1; i++) {
		range[i] += range[i - 1];
		if (i <= range[i] + 1) {
			res = i - 1;
		}
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3856
final_rank: 11

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i] >> B[i];
	}
	int L = *max_element(B.begin(), B.end());
	vector<int> S(L + 2);
	for (int i = 0; i < N; i++) {
		S[A[i]]++;
		S[B[i] + 1]--;
	}
	for (int i = 0; i <= L; i++) {
		S[i + 1] += S[i];
	}
	int ans = 0;
	for (int i = 1; i <= L; i++) {
		if (S[i] >= i - 1) {
			ans = max(ans, i - 1);
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4372
final_rank: 14

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
    int N;
    cin >> N;
    int MAXS = 100003;
    vector<int> COUNT(MAXS,0);
    rep(i,0,N) {
        int A, B;
        cin >> A >> B;
        COUNT[A]++, COUNT[B+1]--;
    }
    rep(i,0,MAXS-1) COUNT[i+1] += COUNT[i];
    int ANS = -1;
    rep(i,1,MAXS) {
        if (COUNT[i] >= i-1) chmax(ANS,i);
    }
    cout << ANS - 1 << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3664
final_rank: 9

code_snippet: #define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <queue>
#include <math.h>
#define LI long int
#define VI vector<int>
#define VVI vector<vector<int>>
#define IPQUE priority_queue<int, vector<int>, greater<int>>
#define DPQUE priority_queue<double, vector<double>, greater<double>>
using namespace std;
int main(){
	int n;
	cin >> n;
	int maxi = 0;
	VI sum(100001);
	int a,b;
	for(int i=0;i<n;i++){
		cin >> a >> b;
		sum[a-1]++;
		if(b != 100001)
			sum[b]--;
	}
	for(int i=0;i<100001;i++){
		if(i > 0)
			sum[i] += sum[i-1];
	}
	for(int i=0;i<100001;i++){
		if(i <= sum[i])
			if(i > maxi)
				maxi = i;
	}
	cout << maxi << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3312
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n, a, b, ans=0, cnt[100010]={};
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a >> b;
        cnt[a]++;
        cnt[b+1]--;
    }
    for(int i=1;i<=100001;i++){
        cnt[i] += cnt[i-1];
        if(cnt[i]>=i-1) ans = i-1;
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 40
memory_kb: 3524
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n, a, b, ans=0, cnt[100010]={};
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a >> b;
        for(int j=a;j<=b;j++){
            cnt[j]++;
            if(cnt[j]==j-1) ans = max(ans, j-1);
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 1810
memory_kb: 3524
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define LL_INF 9223372036854775807
#define INT_INF 2147483647
using namespace std;
using ll = long long;

int main()
{
	int n,ans=0;
	cin >> n;
	vector<int> sound(100010);
	for(int i=0;i<n;i++){
		int a,b;
		cin >> a >> b;
		sound[a]++;
		sound[b+1]--;
	}
	for(int i=0;i<100080;i++){
		sound[i+1]+=sound[i];
	}
	for(int i=0;i<100005;i++){
		if(i<=sound[i]+1)ans=max(ans,i-1);
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3604
final_rank: 5

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

int main() {
    int n;
    cin >> n;
    
    vector<int> p(100010);
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        
        for (int j = a; j <= b; j++) p[j]++;
    }
    
    int ans = 0;
    for (int i = 2; i < 100010; i++) {
        //i-1は自分以外の数
        if(i - 1 <= p[i]) ans = i - 1;
    }
    
    cout << ans << endl;
    
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 3612
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)
#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)
#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)
#define ll long long
#define ld long double
#define print(t) cout << t << endl 
#define all(a)  (a).begin(),(a).end()
// << std::fixed << std::setprecision(0)
const ll INF = 1LL << 60;
 
template<class T> bool chmin(T& a, T b){
 if(a > b){
  a = b;
  return true;
 }
  return false;
}
 
template<class T> bool chmax(T& a, T b){
 if(a < b){
  a = b;
  return true;
 }
  return false;
}
  
ll lpow(ll x,ll n){
  ll ans = 1;
  while(n >0){
    if(n & 1)ans *= x;
    x *= x;
    n >>= 1;
  }
  return ans;
}
 
int main(){
  ll siz = 100005;
  ll n;cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i,n){
    cin >> a[i]  >> b[i];
  }
  vector<ll> dp(siz);
  rep(i,n){
    dp[a[i]]++;
    dp[b[i] + 1]--;

  }
  rep(i,siz-1){
    dp[i + 1] += dp[i];
  }
  ll ans = 1;
  rep(i,siz){
    if(dp[i] >= i-1 ){
      chmax(ans,i);
    }
  }
  cout  << ans -1 << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5484
final_rank: 16

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

using namespace std;

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

	int current = 0;
	for(int i=0;i<=n+1;i++) {
		current += v[i];
		if(i > current) continue;
		ans = max(ans, i-1);
	}
		
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3568
final_rank: 3

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

int main(){
    int N; cin >> N;
    vector<int> p(100100);
    for (int i = 0; i < N; i++){
        int a, b; cin >> a >> b; a--; b--;
        p[a]++;
        p[b + 1]--;
    }
    for (int i = 0; i < 100050; i++){
        p[i + 1] += p[i];
    }
    for (int i = 100050; i >= 0; i--){
        if (p[i] >= i){
            cout << i << endl;
            return 0;
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3636
final_rank: 8

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

int main() {
    const int mx = 100100;
    int N; cin >> N;
    vector S(mx + 1, 0);
    for (int _ = 0 ; _ < N ; _++) {
        int l, r; cin >> l >> r;
        S[l - 1]++;
        S[r]--;
    }
    for (int i = 0 ; i < mx ; i++) S[i + 1] += S[i];
    int ans = 0;
    for (int i = 0 ; i < mx ; i++) if (S[i] >= i) ans = i;
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3608
final_rank: 6

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

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

  constexpr int n_max = 100003;
  std::vector< int > imos(n_max);
  imos[0]++;

  std::vector< int > as(n_max), bs(n_max);
  for (int i = 0; i < n; i++) {
    int a, b;
    std::cin >> a >> b;

    as[i] = a;
    bs[i] = b;
    imos[a]++;
    imos[b + 1]--;
  }

  for (int i = 1; i < n_max; i++) {
    imos[i] += imos[i - 1];
  }

  int ans = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (i <= imos[i]) ans = i - 1;
  }

  std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4208
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,m,i,v[200010],a,b;
int main(void){
    cin>>n;
    m=n;
    while(m--){
        cin>>a>>b;
        for(i=a;i<=b;i++){
            v[i]++;
        }
    }
    ll ans=0;
    for(i=0;i<n+2;i++){
        if(v[i]+1>=i){
            ans=i;
        }
    }
    cout<<ans-1<<"\n";
    return 0;
}
accuracy: 1
time_ms: 1180
memory_kb: 4216
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using i32 = int;

int main() {
    const i32 sz = 100005;
    i32 n; cin >> n;
    vector imos(sz, 0);
    for (i32 _ = 0 ; _ < n ; _++) {
        i32 a, b; cin >> a >> b;
        imos[a]++;
        imos[b + 1]--;
    }
    for (i32 i = 0 ; i < sz - 1 ; i++) {
        imos[i + 1] += imos[i];
    }
    i32 ans = 0;
    for (i32 i = 1 ; i <= n + 1 ; i++) {
        if (imos[i] >= i - 1) {
            ans = i - 1;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3600
final_rank: 4

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i< (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 998244353;
const int inf = (1<<30);
 
int main(){
	int n;
	vector<int> imo(100005);
	cin>>n;
	rep(i,n){
		int a,b; cin>>a>>b;
		imo[a]++;
		imo[b+1]--;
	}
	int ans = 0;

	for(int i = 1; i<=n+1; i++){
		imo[i] += imo[i-1];
		if(i <= imo[i] +1 ) ans = i-1;
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3620
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#define int ll
#define rep(i, N) for(int i = 0; i < (int)(N); ++i)
#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)
#define per(i, N) for(int i = (int)(N)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define YesNo(ans) (ans) ? cout << "Yes\n" : cout << "No\n"
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using point = struct{ ld x, y; };
using State = string::iterator;
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;
template<class T> using vvvvec = vvec<vvec<T>>;

constexpr ld EPS = 1e-10;
ld Pi = acos(-1);
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD = (0) ? 998244353 : 1e9+7;
constexpr int NIL = -1;
constexpr int pm[2] = {1, -1};
constexpr int dy[4] = {0, 1, 0, -1};
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};

ll cel(ll a, ll b){ return (a + b - 1) / b;}
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
ll Lcm(ll a, ll b){ return a / Gcd(a, b) * b;}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}

using Pair = pair<ll, ll>;
using Tpl = tuple<int, int, int>;

bool comp(Pair &A, Pair &B){
    if(A.se < B.se) return true;
    else if(A.se == B.se) return A.fi < B.fi;
    else return false;
}

void Main(){
    int N; cin >> N;
    vec<Pair> AB(N); rep(i, N) cin >> AB[i].fi >> AB[i].se;
    sort(all(AB));

    vec<int> sum(100010);
    rep(i, N){
        sum[AB[i].fi]++, sum[AB[i].se + 1]--;
    }
    rep(i, 100009){
        sum[i + 1] += sum[i];
    }

    ll ans = 0;
    rep(i, 100010){
        if(sum[i] >= i - 1) ans = i - 1;
    }

    cout << ans << endl;
}

signed main(){
    //cin.tie(nullptr);
    //ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5448
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using i32 = int;
using i64 = long long;
using ld = long double;
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b and (a = b, true);}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b and (a = b, true);}
const i64 supl = LONG_LONG_MAX / 2 - 100;
const i32 supi = INT_MAX / 2 - 100;

void main_() {
    i32 n; cin >> n;
    vector imos(1000010, 0);
    for (i32 _ = 0 ; _ < n ; _++) {
        i32 a, b; cin >> a >> b;
        imos[a]++;
        imos[b + 1]--;
    }
    for (size_t i = 0 ; i < imos.size() - 1 ; i++) {
        imos[i + 1] += imos[i];
    }
    i32 ans = 0;
    for (i32 i = 1 ; i <= n + 1 ; i++) {
        if (imos[i] >= i - 1) {
            ans = i - 1;
        }
    }
    cout << ans << endl;
}

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

    main_();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6968
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1e18 + 10
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  int n;
  cin>>n;
  int mp[100010]={};
  rep(i,n){
    int a,b;
    cin>>a>>b;
    mp[a]++;
    mp[b+1]--;
  }
  int now=1;
  int ans=0;
  rep(i,100010){
    now+=mp[i];
    if(i<=now) chmax(ans,i-1);
  }
  cout<<ans<<endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4216
final_rank: 12