stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <iostream>

#include <vector>

#include <map>

#define ll long long

#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 2e5 + 5;



int n;

vector<int> a;

int b[maxn];

bool cmp(int x, int y)

{

    return x > y;

}



int main()

{

    cin >> n;

    for(int i=1; i<=n; i++)

    {

        scanf("%d", &b[i]);

        a.push_back(b[i]);

    }

    sort(a.begin(), a.end(), cmp);

    for(int i=1; i<=n; i++)

    {

        int mid = n/2;

        if(b[i] < a[mid-1])

            printf("%d\n", a[mid-1]);

        else

            printf("%d\n", a[mid]);

    }

    return 0;

}

/****



****/


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<stdlib.h>

int a[200005],temp[200005];

int inc(const void *a, const void *b)

{

    return *(int*)a-*(int*)b;

}

int main()

{

    int n;

    scanf("%d",&n);

    for(int i=0;i<n;i++)scanf("%d",&a[i]);

    for(int i=0;i<n;i++)temp[i]=a[i];

    qsort(a,n,sizeof(int),inc);

    for(int i=0;i<n;i++)

    {

        if(temp[i]>=a[n/2])printf("%d\n",a[n/2-1]);

        else printf("%d\n",a[n/2]);

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main(){

  double x[200000]={},m[200000]={},av=0;

  int min,n,idx,a,b;

  cin>>n;

  for(int i=0;i<n;i++){cin>>x[i];av+=x[i];}

  av/=n;

  for(int i=0;i<n;i++)m[i]=x[i];

  sort(m,m+n);

  a=m[n/2];

  b=m[n/2-1];

  av=(a+b)/2;

  for(int i=0;i<n;i++)

    if(av>=x[i])cout<<a<<endl;

    else cout<<b<<endl;

  return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int main(){

  double av;

  int x[200000]={},m[200000]={},min,n,idx,a,b;

  cin>>n;

  for(int i=0;i<n;i++){cin>>x[i];m[i]=x[i];}

  sort(m,m+n);

  a=m[n/2];

  b=m[n/2-1];

  av=(a+b)/2;

  for(int i=0;i<n;i++)

    if(av>=x[i])cout<<a<<endl;

    else cout<<b<<endl;

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

using namespace std;



int main(){

    int N;

    int A[200000]={};

    cin >> N;

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

        cin >> A[i];

        A[i] -= (i+1);

    }

    sort(A,A+N);

    int b = A[N/2];

    long long sum=0;

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

        sum += abs(A[i] - b);

    }

    cout << sum << endl;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)

#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)

long long mo = 1e9 + 7;

typedef long long ll;

typedef long double ld;

typedef pair<int,int> Pii;

typedef pair<ll,ll> Pll;

template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }

template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }

template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}

ld PI=3.14159265358979323846;



int main(){

    ll N;

    cin >> N;

    vector<ll> A(N);

    rep(i,N){

        cin >> A[i];

        A[i] -= i+1;

    }

    sort(A.begin(), A.end());

    ll mid = A[N/2];

    ll ans = 0;

    rep(i,N){

        ans += abs(A[i] - mid);

    }

    cout << ans << endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<cstdlib>

#include<vector>

#include<algorithm>

#include<functional>



#define REP(i,n) for(int i=0;i<n;i++)

#define ll long long int



using namespace std;



int to_int(string s) {

  return atoi(s.c_str());

}



//vector<ll> a;



int main() {

  int n;

  cin >> n;



 // a.reserve(n);

  vector<int> a(300000);

  for(int i=0;i<n;i++) {

    cin >> a[i];

    a[i] -= i+1;

  }



  sort(a.begin(), a.begin()+n);

  long long  b;

  if(n%2 == 0) {

    b = a[n/2 -1];

  } else {

    b = a[(n-1)/2];

  }



  long long  r = 0;



  for(int i=0;i<n;i++) {

    r += abs(a[i] - b);

  }



  cout << r << endl;

return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<string>

#include<cstdlib>

#include<vector>

#include<algorithm>

#include<functional>



#define REP(i,n) for(int i=0;i<n;i++)

#define ll long long int



using namespace std;



int to_int(string s) {

  return atoi(s.c_str());

}



//vector<ll> a;



int main() {

  int n;

  cin >> n;



 // a.reserve(n);

  vector<int> a;

  a.reserve(300000);

  for(int i=0;i<n;i++) {

    cin >> a[i];

    a[i] -= i+1;

  }



  sort(a.begin(), a.begin()+n);

  long long  b;

  if(n%2 == 0) {

    b = a[n/2 -1];

  } else {

    b = a[(n-1)/2];

  }



  long long  r = 0;



  for(int i=0;i<n;i++) {

    r += abs(a[i] - b);

  }



  cout << r << endl;

return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include "bits/stdc++.h"

using namespace std;

#define MOD 1000000007

#define Nmax 200010

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)

#define RFOR(i,a,b) for(long long i = (b-1);i>=a;i--)

#define REP(i,n)  FOR(i,0,n)

#define RREP(i,n) RFOR(i,0,n)

#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)

#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)

#define dump(x)  cout << #x << " = " << (x) << endl;

#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

typedef long long ll;

typedef pair<ll,ll> P;

typedef vector<P> Graph;

 

int main() {

	int N;

	cin >> N;

	ll A[Nmax]={};

	ll sumA=0;

	REP(i,N){

		cin >> A[i];

		A[i]-=i+1;

		sumA+=A[i];

	}

	ll S[Nmax]={};

  	sort(A,A+N);

  	S[0]=A[0];

  	REP(i,N-1) S[i+1]+=S[i]+A[i+1];

  	//sort(S,S+N);

	ll b=round((double)(sumA/N)); //仮のb

	ll ans1=0,ans2=0;

	ll mina=0;

	REP(i,N) mina+=abs(A[i]-b);

	FOR(j,1,MOD){

		/*REP(i,N){

      	ans1+=abs(A[i]-b+j);//bを上下に1ずつ広げていく

	  	ans2+=abs(A[i]-b-j);

   		}*/

   		int a=upper_bound(A,A+N,b+j)-A;

   		int c=upper_bound(A,A+N,b-j)-A;

   		ans1=abs(S[a-1]-(b+j)*(a))+abs((S[N-1]-S[a-1])-(b+j)*(N-a));

   		ans2=abs(S[c-1]-(b-j)*(c))+abs((S[N-1]-S[c-1])-(b-j)*(N-c));

   		mina=min(mina,min(ans1,ans2));

   		if(mina!=ans1&&mina!=ans2){//単調増加に転じたらbreak

	   		break;

	   	}

   	}

	cout << mina << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include "bits/stdc++.h"

using namespace std;

#define MOD 1000000007

#define Nmax 200010

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)

#define RFOR(i,a,b) for(long long i = (b-1);i>=a;i--)

#define REP(i,n)  FOR(i,0,n)

#define RREP(i,n) RFOR(i,0,n)

#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)

#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)

#define dump(x)  cout << #x << " = " << (x) << endl;

#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

typedef long long ll;

typedef pair<ll,ll> P;

typedef vector<P> Graph;

 

int main() {//答え聞いた後版

	int N;

	cin >> N;

	ll A[Nmax]={};

	REP(i,N){

		cin >> A[i];

		A[i]-=i+1;

	}

  	sort(A,A+N);

	ll b=A[(N-1)/2];//med

	ll mina=0;//ans

	REP(i,N) mina+=abs(A[i]-b);



	cout << mina << endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//#include "bits/stdc++.h"



#include <iostream>

#include <array>

// #include <map>

// #include <string>

// #include <algorithm>

// #include <numeric>

// #include <limits>



using namespace std;



array<int, 100002> box; // use key 100001



int n;

int min_;

int max_;



int solve() {

	cin >> n;

	for (int i = 0; i < n; i++) {

		cin >> min_ >> max_;

		for (int j = min_; j <= max_; j++) {

			box[j]++;

		}

	}

	for (int i = n + 1; i >= 0; i--) {

		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug

		if (i - 1 <= box[i]) {

			return i - 1;

		}

	}

	return 0;

}



int main() {



	cout << solve() << endl;



	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug

	return 0;

}

// Optimized solution to the same problem (faster):
//#include "bits/stdc++.h"



#include <iostream>

#include <array>

// #include <map>

// #include <string>

// #include <algorithm>

// #include <numeric>

// #include <limits>



using namespace std;



array<int, 100002> box; // use key 100001



int n;

int np;

int min_;

int max_;



int solve() {

	cin >> n;

	np = n + 1;

	for (int i = 0; i < n; i++) {

		cin >> min_ >> max_;

		if (min_ > np) {

			continue;

		}

		box[min_ - 1]--;

		max_ = min(max_, np);

		box[max_]++;

	}

	box[np + 2] = 0;

	for (int i = np + 1; i >= 0; i--) {

		box[i] += box[i+1];

		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug

		if (i - 1 <= box[i]) {

			return i - 1;

		}

	}

	return 0;

}



int main() {



	cout << solve() << endl;



	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;



int N;

int a,b,ans=0;

int P[100010]={};



int main(){

  cin >> N;

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

    cin  >> a >> b;

    for(int j=a-1;j<=b-1;j++){

      P[j]+=1;

    }

  }

  for(int i=0;i<=N+1;i++){

    //cout << P[i] << endl;

  }

  for(int i=1;i<=N+1;i++){

    //cout << P[i] << endl;

    //ans = max(ans,P[i]);

    if(P[i]>=i){

      ans = max(ans,i);

    }

    else if(P[i]==i){

      ans = max(ans,P[i]);

    }

    //cout << i <<":" << ans << endl;

  }

  //if(ans!=0) ans-=1;

  cout << ans << endl;

}




// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

#define int long long

int N,answer;

int table[100000+10],ans[100000+10];

signed main(){

  cin >> N;

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

      int a,b;

      cin >> a >> b;

      a--,b--;

      table[a]++;

      table[b+1]--;

    }

    for(int i=1;i<=N;i++){

      ans[i]+=ans[i-1]+table[i];

      //cout << ans[i] << endl;

      if(i<=ans[i]) answer=i;

    }

    cout << answer << endl;

  return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define N 100005

using namespace std;

int n,a,b,d[N],ans;



int main(){

  cin>>n;

  for(int i=0;i<n;i++){

    cin>>a>>b;

    d[a]++;

    d[b+1]--;

  }

  for(int i=1;i<=n+1;i++)d[i]+=d[i-1];

  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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <map>

#include <algorithm>

 

#define rep(i,n) for (int i = 0; i < (n); ++i)

  

int n, a, b, friends[100005], fri, maxi;



int main() {

  std::cin.tie(0);

  std::ios::sync_with_stdio(false);



  std::cin >> n;

  rep(i,n) {

    std::cin >> a >> b;

    a--; b--;

    for (int i = a; i <= b; ++i) friends[i]++;

  }

  rep(i,n+1) {

    if (friends[i] >= i) {

        maxi = i;

    }

  }



  std::cout << maxi << std::endl;



  return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <vector>

#include <map>

#include <algorithm>



#define rep(i,n) for (int i = 0; i < (n); ++i)



int n, a, b, friends[100005], fri, ans;



/***************************************



           imos???version



***************************************/



int main() {

  std::cin.tie(0);

  std::ios::sync_with_stdio(false);



  std::cin >> n;

  rep(i,n) {

    std::cin >> a >> b;

    a--;

    friends[a]++;

    friends[b]--;

  }



  for (int i = 1; i <= n; ++i) friends[i] += friends[i-1];



  rep(i,n+1) {

      if (friends[i] >= i) {

          ans = std::max(ans, i);

      }

  }



  std::cout << ans << std::endl;



  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?



#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;

}

// Optimized solution to the same problem (faster):
// #define _GLIBCXX_DEBUG // for STL debug (optional)

#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 <fstream>

#include <functional>

#include <bitset>

using namespace std;

using ll = long long int;

using int64 = 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;}

 

int dx[] = {0, 0, 1, -1};

int dy[] = {1, -1, 0, 0};

const int INF = 1LL << 29;

const ll LONGINF = 1LL << 60;

const ll MOD = 1000000007LL;

 

int main() {

    int N; scanf("%d", &N);

    const int MAXN = 100010;

    vector<int> seg(MAXN+5);

    seg[0] = 1;

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

        int a, b;

        scanf("%d%d", &a, &b);

        seg[a]++, seg[b+1]--;

    }



    for(int i=1; i<=MAXN+3; i++) seg[i] += seg[i-1];



    int ans = -1;

    for(int i=1; i<=MAXN; i++) {

        if(seg[i] >= i) {

            chmax(ans, i-1);

        }

    }

    printf("%d\n", ans);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <sstream>

#include <fstream>

#include <string>

#include <vector>

#include <deque>

#include <queue>

#include <stack>

#include <set>

#include <map>

#include <algorithm>

#include <functional>

#include <utility>

#include <bitset>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <cstdio> 

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)

#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)



int canGo[100001] = {0};



class Solve {

public:

	Solve() {};

	~Solve() {};



	void solver() {

		int ans = 0;

		int n;

		std::cin >> n;

		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));

		rep(i, n) {

			std::cin >> friends[i].first >> friends[i].second;

			//Exclude myself

			friends[i].first--; friends[i].second--;

		}

		//Record the number of people who can go.

		for (auto& x : friends) {

			rangeRep(i, x.first, x.second+1) {

				canGo[i]++;

			}

		}

		int max = 0;

		rep(i, n+1) {

			if (canGo[i] >= i) {

				max = i;

			}

		}

		std::cout << max << std::endl;

	}

private:



};



int main() {

	Solve solve;

	solve.solver();

	return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <sstream>

#include <fstream>

#include <string>

#include <vector>

#include <deque>

#include <queue>

#include <stack>

#include <set>

#include <map>

#include <algorithm>

#include <functional>

#include <utility>

#include <bitset>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <cstdio> 

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)

#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)



int canGo[100001] = {0};



class Solve {

public:

	Solve() {};

	~Solve() {};



	void solver() {

		int ans = 0;

		int n;

		std::cin >> n;

		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));

		rep(i, n) {

			std::cin >> friends[i].first >> friends[i].second;

			//Exclude myself

			friends[i].first--; friends[i].second--;

		}

		//Record the number of people who can go.

		for (auto& x : friends) {

			canGo[x.first]++;

			canGo[x.second + 1]--;

		}

		//fill

		rangeRep(i, 1, n + 1) {

			canGo[i] += canGo[i - 1];

		}

		int max = 0;

		rep(i, n+1) {

			if (canGo[i] >= i) {

				max = i;

			}

		}

		std::cout << max << std::endl;

	}

private:



};



int main() {

	Solve solve;

	solve.solver();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>



using namespace std;



const int MAX = 100005;

int dp[MAX];



int main(){



  int n;

  scanf("%d",&n);



  while(n--){

    int a,b;

    scanf("%d %d",&a,&b);

    for(int i = a; i <= b; i++) dp[i]++;

  }



  for(int i = MAX-1; i>0; i--)

    if(dp[i] >= i-1){

      printf("%d\n",i-1);

      break;

    }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>



using namespace std;



const int MAX = 100005;

int res[MAX];



int main(){



  int n;

  scanf("%d",&n);



  while(n--){

    int a,b;

    scanf("%d %d",&a,&b);

    res[a-1]++;

    res[b]--;

  }



  int now = 0;

  int ans = 0;

  for(int i = 0; i < MAX; i++){

    now += res[i];

    if(now >= i) ans = i;

  }



  printf("%d\n",ans);

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstring>

#include <algorithm>

using namespace std;



int imos[111111];

int main(){

    int n;

    while(cin >> n){

        //memset(imos,0,sizeof(imos));

        for(int i=0;i<n;i++){

            int a,b;

            cin>>a>>b;

            imos[a-1]++;

            imos[b]--;

        }

        for(int i=1;i<111111;i++){

            imos[i]+=imos[i-1];

        }

        int ans=0;

        for(int i=0;i<111111;i++){

            if(imos[i]>=i)ans=max(ans,i);

        }

        cout << ans << endl;

    }

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

using namespace std; 

const int INF = 1e9;



int main(){

    int n;

    while(cin >> n){

        int diff[100010] = {};

        rep(i,n){

            int a,b;

            cin >> a >> b;

            ++diff[a-1];

            --diff[b];

        }

        int x = 0;

        int ans = 0;

        rep(i,100010){

            x += diff[i];

            if(x >= i) ans = i;

        }

        cout << ans << endl;

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for (int i = a; i < b; i++)

#define REP(i,b) FOR(i,0,b)

#define RFOR(i,a,b) for (int i = a-1; i >= b; i--)

#define RREP(i,a) RFOR(i,a,0)

#define REPALL(i,v) for (int i = 0; i < v.size(); i++)

#define RREPALL(i,v) for (int i = v.size()-1; i >= 0; i--)

#define SORT(v) sort(v.begin(), v.end())

#define MIN_ELEMENT(v) min_element(v.begin(), v.end())

#define MAX_ELEMENT(v) max_element(v.begin(), v.end())

#define COUNT(v,n) count(v.begin(), v.end(), n);

void YES(bool flag) {cout<<(flag ? "YES" : "NO")<<endl;}

void Yes(bool flag) {cout<<(flag ? "Yes" : "No")<<endl;}

void yes(bool flag) {cout<<(flag ? "yes" : "no")<<endl;}

typedef long long ll;

typedef unsigned long long ull;

const int INF = 1e7;

const ll MOD = 1e9 + 7;



vector<int> a,b, sum(111111);

int ans = 1;



int main()

{

    int n;

    cin>>n;

    a.resize(n); b.resize(n);

    REP(i,n) {

        cin>>a[i]>>b[i];

        sum[a[i]]++;

        sum[b[i]+1]--;

    }

    sum[0]++;

    REP(i,100111) {

        sum[i+1] = sum[i] + sum[i+1];

        if (sum[i+1] >= i+1) {

            ans = i+1;

        }

    }

    cout<<ans - 1<<endl;



    return 0;

}




// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



#define CEIL(a,b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))



using namespace std;

using ll = long long;

using pii = pair<int, int>;



constexpr int MOD = 1'000'000'007;

constexpr int INF = 1'000'000'001;

constexpr ll LLINF = 4'000'000'000'000'000'001;

// constexpr int INF = 2147483647; // 2 * 1e9

// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18



const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};

const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};



int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    int n;

    cin >> n;

    vector<int> t(100002);

    for(int i = 0; i < n; ++i){

        int a, b;

        cin >> a >> b;

        --a; --b;

        t[a] += 1;

        t[b+1] -= 1;

    }

    for(int i = 0; i <= n+1; ++i){

        t[i+1] += t[i];

    }

    for(int i = n + 1; i >= 0; --i){

        if(t[i] >= i){

            cout << i << "\n";

            return 0;

        }

    }

    cout << "0\n";

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;



int main(){

  int n;



  while(cin>>n){

    int dec[100002];

    int inc[100002];



    for(int i=0;i<100002;i++){

      dec[i] = 0;

      inc[i] = 0;

    }



    for(int i=0;i<n;i++){

      int a,b;

      cin>>a>>b;

      inc[a]++;

      dec[b]++;

    }



    int now = 0;

    int ans = 0;



    for(int i=0;i<100002;i++){

      now += inc[i];

      if(now + 1 >= i) ans = max(ans, i);

      now -= dec[i];

    }



    cout<<ans-1<<endl;

  }

}

// Optimized solution to the same problem (faster):


#include <iostream>

#include <cstring>

#include <cstdio>

using namespace std;



int n;

int pls[100002], mns[100002];



int main(){

  while(scanf("%d", &n) != EOF){

    memset(pls, 0, sizeof(pls));

    memset(mns, 0, sizeof(mns));



    for(int i = 0; i < n; i++){

      int a, b;

      scanf("%d%d", &a, &b);

      pls[a]++;

      mns[b]++;

    }



    int sum = 0;

    int ans = 1;



    for(int i = 1; i <= n + 1; i++){

      sum += pls[i];



      if(i <= sum + 1){

        ans = i;

      }



      sum -= mns[i];

    }



    cout << ans - 1 << endl;

  }

}

// Fastest known solution for this problem: