Problem:
Dungeon
Bob is playing a popular game called "Dungeon". The game is played on a rectangular board consisting of
W ×  H
squares. Each square is identified with its column and row number, thus the square located in the
x
-th column and the
y
-th row is represented as (
x
,
y
). The left-most square in the top row is (0, 0) and the right-most square in the bottom row is (
W
-1,
H
-1).
Bob moves a character "BomBom" to clear the game. BomBom is initially located at (0, 0). The game is won if Bob successfully destroys all the enemy characters on the board by manipulating BomBom cleverly. The enemy characters are fixed on specific squares, and Bob can manipulate BomBom using the following two operations any number of times.
One-square movement in the up, down, left, or right direction within the board
Using a bomb, eliminate all the enemy characters that are located in the same column and row as that of BomBom
BomBom consumes a Cost when it moves from one square to another. BomBom can use a bomb any number of times without consuming a Cost. Use of a bomb has no effect on BomBom’s behavior and it can move even to a square where an enemy character is located.
Given the board size and enemy information, make a program to evaluate the minimum Cost BomBom consumes before it destroys all enemy characters.
Input
The input is given in the following format.
W
H
N
x_1
y_1
x_2
y_2
:
x_N
y_N
The first line provides the number of squares in the horizontal direction
W
(1 ≤
W
≤ 10
5
), in the vertical direction
H
(1 ≤
H
≤ 10
5
), and the number of enemy characters
N
(1 ≤
N
≤ 10
5
). Each of the subsequent
N
lines provides location information of the
i
-th enemy, column
x_i
(0 ≤
x_i
≤
W
-1) and row
y_i
(0 ≤
y_i
≤
H
-1). The number of enemy characters in a specific square can be either one or zero.
Output
Output the minimum Cost in a line.
Sample Input 1
5 4 4
0 3
1 1
2 2
2 3
Sample Output 1
2
Sample Input 2
6 6 5
2 1
5 2
3 3
1 4
1 5
Sample Output 2
4
Sample Input 3
8 8 4
6 0
7 0
0 6
0 7
Sample Output 3
0

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(int i=0;i<(ll)(N);i++)
#define fi first 
#define se second
#define all(X) (X).begin(),(X).end()
ll gcd(ll a,ll b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int main(){
    ll W,H,N;cin>>W>>H>>N;
    vector<pair<ll,ll>>X(N);
    rep(i,N)cin>>X[i].fi>>X[i].se;
    sort(all(X));
    vector<ll>M(N+1,0);
    for(int i = N-1;i>=0;i--){
        M[i] = max(M[i+1],X[i].se);
    }
    ll ans = M[0];
    rep(i,N){
        ans = min(ans,X[i].fi+M[i+1]);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5436
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
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; }
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    int w, h, n; cin >> w >> h >> n;
    int sz = 1e5;
    vector<int> MX(sz + 1);
    rep(i, n) {
        int x, y; cin >> x >> y;
        if(y)chmax(MX[y - 1], x);
    }
    for (int i = sz - 1; i >= 0; --i)chmax(MX[i], MX[i + 1]);
    int ans = inf;
    rep(i, sz + 1) {
        chmin(ans, i + MX[i]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3712
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define all(a) (a).begin(),(a).end()

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    int W, H, N;
    cin >> W >> H >> N;
    
    vector<int> A(H);
    rep(i, 0, N) {
        int x, y;
        cin >> x >> y;
        A[y] = max(A[y], x);
    }
    
    for (int i = H - 2; i >= 0; --i) A[i] = max(A[i], A[i + 1]);

    int ans = H - 1;
    rep(i, 0, H - 1) {
        ans = min(ans, i + A[i + 1]);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3580
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
	int W, H, N;cin >> W >>H >> N;
	vector<pair<int, int>> st;
	int res = (int)1e9;
	for (int i = 0;i < N;i++) {
		int x, y;cin >> x >> y;
		st.push_back({x, y});

	}
	sort(st.begin(), st.end());
	vector<int> LMX(N, 0);
	int mx = 0;
	for (int i = N-1;i >= 0;i--) {
		mx = max(mx, st[i].second);
		LMX[i] = mx;
	}
	res = mx;
	for (int i = 0;i < N;i++) {
		if (i+1 == N) {
			res = min(res, st[i].first);continue;
		}
		res = min(res, st[i].first + LMX[i+1]);
	}
	cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4560
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll MOD=10000000;
const ll INF=1000000001;
const ll LINF=4000000000000000010;
const double EPS=1e-9;
int dx[5]={0,1,0,-1,0};
int dy[5]={1,0,-1,0,0};
vector<int> v[100010];
int ma[100010];
int main(){
    int w,h,n;cin>>w>>h>>n;
    for(int i=0;i<n;i++){
        int x,y;cin>>x>>y;
        v[x].push_back(y);
    }
    for(int i=w-1;i>=1;i--){
        int maa=0;
        for(auto a:v[i]){
            maa=max(a,maa);
        }
        ma[i-1]=max(ma[i],maa);
    }
    int ans=INF;
    for(int i=0;i<w;i++){
        int s=i+ma[i];
        ans=min(s,ans);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 9304
final_rank: 18

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

#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int W, H, N;
    cin >> H >> W >> N;
    multiset<int> XS, YS;
    vector<vector<int>> X(H), Y(W);
    for (int i = 0; i < N; i++) {
        int x, y;
        cin >> x >> y;
        X[x].emplace_back(y);
        Y[y].emplace_back(x);
        XS.insert(x);
        YS.insert(y);
    }

    int ans = min(H, W);
    // x 座標を走査
    for (int i = 0; i < H; i++) {
        // x 座標が [0, i + 1) は駆逐
        for (auto y : X[i]) YS.erase(YS.find(y));
        // それ以外での y 座標の max
        ans = min(ans, i + (YS.empty() ? 0 : *YS.rbegin()));
    }

    // y 座標を走査
    for (int i = 0; i < W; i++) {
        // y 座標が [0, i + 1) は駆逐
        for (auto x : Y[i]) XS.erase(XS.find(x));
        // それ以外での x 座標の max
        ans = min(ans, i + (XS.empty() ? 0 : *XS.rbegin()));
    }

    cout << ans << '\n';
}
accuracy: 1
time_ms: 100
memory_kb: 20900
final_rank: 20

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

template<typename T,T e>
struct segtree {
	vector<long long> v;
	long long n;
	T f(T a,T b){
		return max(a,b);
	}
	segtree(long long n) : n(n) {
		v.resize(n * 2ll);
		for(long long i = 0ll;i < n * 2ll;i++){
			v[i] = e;
		}
	}
	segtree(vector<T> x) : n(x.size()) {
		v.resize(n * 2ll);
		for(long long i = 0ll;i < n;i++){
			v[i + n] = x[i];
		}
		for(long long i = n - 1;i >= 0;i--){
			v[i] = f(v[i * 2ll],v[i * 2ll + 1ll]);
		}
	}
	T operator[](long long a){
		return v[a + n];
	}
	void set(long long a,long long b){
		a += n;
		v[a] = b;
		while(a >>= 1ll){
			v[a] = f(v[a * 2ll],v[a * 2ll + 1ll]);
		}
	}
	T prod(long long l,long long r){
		T res = e;
		for(l += n,r += n;l < r;l >>= 1ll,r >>= 1ll){
			if(l & 1ll){
				res = f(res,v[l++]);
			}
			if(r & 1ll){
				res = f(res,v[--r]);
			}
		}
		return res;
	}
};
int main()
{
	int w,h,n;
	cin >> w >> h >> n;
	vector<pair<int,int>> a(n,pair<int,int>());
	segtree<int,0> seg(w + 1);
	for(int i = 0;i < n;i++){
		cin >> a[i].first >> a[i].second;
		seg.set(a[i].first,max(seg[a[i].first],a[i].second));
	}
	int ans = INT_MAX;
	for(int i = 0;i <= w;i++){
		int it = seg.prod(i + 1,w + 1);
		ans = min(ans,i + it);
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5460
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
P pp[100000];
int main(void)
{
	int w,h,n,mx,i,my,mm;
	cin >>w>>h>>n;
	for(i=0;i<n;i++)	{
		cin >>pp[i].first>>pp[i].second;
	}
	sort(pp,pp+n);
	mx=pp[n-1].first;
	my=0;
	mm=mx+my;
	for(i=n-2;i>=0;i--)	{
		mx=pp[i].first;
		my=max(my,pp[i+1].second);
		mm=min(mm,mx+my);
	}
	my=max(my,pp[0].second);
	mm=min(mm,my);
	cout <<mm<<endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3880
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;

int n;
string s;

int main(){
    //
    int w,h,n;
    cin >> w >> h >> n;
    
    vector<pair<int,int>> graph(n);
    for (int i = 0;i < n;i++){
        cin >> graph[i].first >> graph[i].second;
    }
    sort(graph.begin(),graph.end());

    vector<int> max_x(w);
    int cnt = 0;
    int ans = w;
    for (int i = n - 1;i >= 0;i--){
        ans = min(ans,graph[i].first + cnt);
        cnt = max(cnt,graph[i].second);
    }
    ans = min(ans,cnt);
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4048
final_rank: 6

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

int main() {
    int w, h, n;
    cin >> w >> h >> n;
    int x[n], y[n];
    int Max[w] = {};
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
        Max[x[i]] = max(Max[x[i]], y[i]);
    }
    for (int i = w - 2; i >= 0; i--) {
        Max[i] = max(Max[i], Max[i + 1]);
    }
    int Min = w - 1;
    for (int i = 0; i < w - 1; i++) { //曲がるところ
        Min = min(Min, Max[i + 1] + i);
    }
    cout << Min << "\n";
    return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 4608
final_rank: 11

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

int main() {
  int N;
  cin >> N >> N >> N;
  vector<pair<int, int>> p(N);
  for(int i = 0; i < N; i++) {
    cin >> p[i].first >> p[i].second;
  }
  sort(p.begin(), p.end());
  vector<int> mx(N+1);
  for(int i = N-1; i >= 0; i--) {
    mx[i] = max(mx[i+1], p[i].second);
  }
  int ans = mx[0];
  for(int i = 0; i < N; i++) {
    ans = min(ans, p[i].first+mx[i+1]);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4160
final_rank: 7

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

struct cell {
	int x, y;
};

int main() {
	int W, H, N;
	cin >> W >> H >> N;
	vector<cell> C(N);
	for (int i = 0; i < N; i++) {
		cin >> C[i].x >> C[i].y;
	}
	sort(C.begin(), C.end(), [](const cell& c1, const cell& c2) {
		return c1.x != c2.x ? c1.x < c2.x : c1.y < c2.y;
	});
	vector<cell> st;
	for (cell c : C) {
		while (!st.empty() && st.back().y <= c.y) {
			st.pop_back();
		}
		st.push_back(c);
	}
	int answer = min(st[0].y, st.back().x);
	for (int i = 1; i < st.size(); i++) {
		answer = min(answer, st[i - 1].x + st[i].y);
	}
	cout << answer << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4188
final_rank: 8

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

int main() {
	int H, W, N;
	cin >> H >> W >> N;
	vector<vector<int>> A(H + 1);
	for (int i = 0; i < N; i++){
		int x, y;
		cin >> x >> y;
		A[x].push_back(y);
	}
	int ans = 1e9;
	int m = 0;
	for (int i = H; i >= 0; i--){
		ans = min(ans, i + m);
		for (int j : A[i]){
			m = max(m, j);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 8428
final_rank: 17

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define repp(i,a,n) for(long long i=a;i>=n;i--)
#define so(z) sort(z.begin(),z.end())
#define sor(z) sort(z.rbegin(),z.rend())
#define re(z) reverse(z.begin(),z.end())
#define su(a) substr(a)
#define sub(a,b) substr(a,b)
#define yess cout<<"YES"<<nn
#define noo cout<<"NO"<<nn
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define yesr cout<<"Yes"<<"\n";return(0)
#define nor cout<<"No"<<"\n";return(0)
#define fi first
#define se second
#define fr() front()
#define mp make_pair
#define pb push_back
#define nn "\n"
#define ll long long
#define mod 1000000007
#define IM INT_MAX
#define LM LONG_LONG_MAX
#define gcd(x,y) __gcd(x,y) //最大公約数
#define lcm(x,y) x/(__gcd(x,y))*y //最小公倍数
#define prev(a) prev_permutation(a.begin(),a.end()) //辞書前
#define next(a) next_permutation(a.begin(),a.end()) //辞書後
#define get(s) getline(cin,s) //　空白の空いたstringの入力
#define fixed(a) fixed<<setprecision(a) //小数点何桁か指定
#define ro(a) round(a) //四捨五入
#define ce(a) ceil(a) //切り上げ
#define up(a,v) upper_bound(a.begin(),a.end(),v)
#define lo(a,v) lower_bound(a.begin(),a.end(),v)
#define bi(a,v) binary_bound(a.begin(),a.end(),v)
#define pp priority_queue<pair<long long,long long>, vector<pair<long long,long long>>, greater<pair<long long,long long>> > // priority_queue
using namespace std;
int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    ll w,h,n,ans=LM;
    cin>>w>>h>>n;
    vector<ll>x(n),y(n),z(h+1,0);
    rep(i,0,n){
        cin>>x[i]>>y[i];
        z[y[i]]=max(z[y[i]],x[i]);
    }
    /*
    rep(i,0,h){
        cout<<z[i]<<" ";
    }
    cout<<nn;
    */
    repp(i,h-2,0){
        z[i]=max(z[i],z[i+1]);
    }
    /*
    rep(i,0,h){
        cout<<z[i]<<" ";
    }
    cout<<nn;
    */
    rep(i,0,h){
        ans=min(ans,z[i+1]+i);
    }
    cout<<ans<<nn;
    return(0);
}
/*
00000
0x000
x0x00
00x00

000000
00x000
00000x
000x00
0x0000
0x0000

*/
accuracy: 1
time_ms: 10
memory_kb: 5128
final_rank: 3

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0364.cc: Dungeon
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_H = 100000;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int mxs[MAX_H + 1];

/* subroutines */

/* main */

int main() {
  int w, h, n;
  scanf("%d%d%d", &w, &h, &n);

  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    mxs[y] = max(mxs[y], x);
  }

  for (int y = h - 1; y >= 0; y--)
    mxs[y] = max(mxs[y], mxs[y + 1]);
  //for (int y = 0; y <= h; y++) printf("%d ",mxs[y]); putchar('\n');

  int minl = INF;
  for (int y = 0; y < h; y++) {
    int l = y + mxs[y + 1];
    minl = min(minl, l);
  }

  printf("%d\n", minl);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>
using namespace std;
using ll = long long;

ll ll_gcd(ll a, ll b) {
	if (a < b) return ll_gcd(b, a);
	ll r;
	while ((r = a % b)) {
		a = b;
		b = r;
	}
	return b;
}

bool x(vector<ll>& z) {
	ll now = 0;
	for (int i = 0; i < z.size(); i++) {
		if (now < i * 10)return false;
		now = max(now, i * 10 + z[i]);
	}
	return true;
}

int main(){
	ll w, h, n;
	cin >> w >> h >> n;
	n++;
	vector<pair<ll,ll>> z(n);
	for (int i = 0; i < n-1; i++) {
		cin >> z[i].first >> z[i].second;
	}
	z[n-1] = make_pair(0, 0);
	sort(z.begin(), z.end());
	reverse(z.begin(), z.end());
	ll ans = 999999999;
	ll nmax = 0;
	for (int i = 0; i < n; i++) {
		ans = min(ans, nmax + z[i].first);
		nmax = max(nmax, z[i].second);
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4664
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<int, int>;

template <typename T>
struct FenwickTree{
    const int n;
    vector<T> arr;
    FenwickTree() = default;
    FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}

    void add(int ind, T x){
        for(int i = ind+1; i <= n; i += i & (-i)){
            arr[i] += x;
        }
    }

    T sum_sub(int end){
        T res = 0;
        for(int i = end; i > 0; i -= i & (-i)){
            res += arr[i];
        }
        return res;
    }

    T sum(int start, int end){
        return sum_sub(end) - sum_sub(start);
    }

    int lower_bound(T w){
        if(w <= 0) return 0;
        int x = 0;
        int k = 1 << 30;
        while(k > n) k /= 2;
        for(; k > 0; k /= 2){
            if(x+k <= n && arr[x+k] < w){
                w -= arr[x+k];
                x += k;
            }
        }
        return x;
    }
};

int main(){
    int w, h, n;
    cin >> w >> h >> n;
    vector<P> ps(n);
    for(auto &it: ps) cin >> it.first >> it.second;

    FenwickTree<int> ft(100010);
    for(auto &it: ps) ft.add(it.second, 1);
    sort(ps.begin(), ps.end());
    int ans = ft.lower_bound(ft.sum(0, 100010));
    for(auto &it: ps){
        ft.add(it.second, -1);
        ans = min(ans, it.first+ft.lower_bound(ft.sum(0, 100010)));
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4316
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
int main(){
  int W, H, N;
  cin >> W >> H >> N;
  vector<int> x(N), y(N);
  for (int i = 0; i < N; i++){
    cin >> x[i] >> y[i];
  }
  vector<int> d(W, 0);
  for (int i = 0; i < N; i++){
    if (x[i] > 0){
      d[x[i] - 1] = max(d[x[i] - 1], y[i]);
    }
  }
  for (int i = W - 2; i >= 0; i--){
    d[i] = max(d[i], d[i + 1]);
  }
  int ans = INF;
  for (int i = 0; i < W; i++){
    ans = min(ans, i + d[i]);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4312
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rep2(i,s,n) for (int i = s; i < (int)(n); i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
typedef long long ll;
typedef pair<ll,ll> P;
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
if(x<y)return x;
else return y;
}


int main(){
    int h,w,n;
    cin >> w >> h >> n;
    vector<int> pl(w,-1);
    rep(i,n){
        int x,y;
        cin >> x >> y;
        pl[x]=max(pl[x],y);
    }
    priority_queue<P,vector<P>,greater<P>> q;
    rep(j,w)if(pl[j]!=-1){
        while(q.size() && q.top().first<=pl[j])q.pop();
        q.emplace(pl[j],j);
    }
    vector<P> res;
    while(q.size()){
        res.push_back(q.top());
        q.pop();
    }
    sort(all(res),[&](P a,P b){return a.second<b.second;});
    if(res.size()==1){
        cout << min(res[0].first,res[0].second) << endl;
        return 0;
    }
    int ans=1000000000;
    rep(idx,res.size()-1){
        ans=min(ans,res[idx].second+res[idx+1].first);
    }
    ans=min(ans,res[0].first);
    ans=min(ans,res.back().second);
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4532
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
using namespace std;
using ll = long long;

int main() {
    int w, h, n;
    cin >> w >> h >> n;
    map<int, int> b;
    rep(i, n) {
        int x, y; cin >> x >> y;
        if (b.count(y)) {
            b[y] = max(b[y], x);
        } else {
            b[y] = x;
        }
    }
    int max_x = 0;
    int ans = 1e9;
    for (int y=100010; y >= 0; y--) {
        int dist = y + max_x;
        ans = min(ans, dist);
        if (b.count(y)) {
            max_x = max(max_x, b[y]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 7756
final_rank: 19