Problem:
展覧会 2
JOI 美術館には，東西方向にまっすぐに伸びる廊下に
N
枚の絵が飾られており，
1
から
N
までの番号が付けられている．絵
i
(
1 ≦ i ≦ N
) は廊下の西端から
X
i
メートルの位置に飾られており，その価値は
V
i
である．
この美術館では明日から「エゴイ展」が開催される予定であり，非常に多くの来客が見込まれている．「エゴイ展」では
M
枚の絵を展示する予定である．
2
つの絵が近い位置に展示されていると見づらいので，以下の条件を満たすように
N-M
枚の絵を取り外し，廊下に
M
枚の絵だけを残すことにした．
どの
2
つの絵についても，位置が
D
メートル以上離れているようにする．
展示されている
M
枚の絵の価値の最小値を，「エゴイ展」の
華やかさ
とする．あなたは，廊下に残す
M
枚の絵をうまく選ぶことで，「エゴイ展」の華やかさをできるだけ大きくしたい．
N
枚の絵の情報と廊下に残す絵の枚数が与えられたとき，条件を満たすような絵の残し方が存在するか判定し，もし存在する場合は，「エゴイ展」の華やかさの最大値を求めるプログラムを作成せよ．
制約
1 ≦ N ≦ 100000
．
1 ≦ M ≦ N
．
1 ≦ D ≦ 1000000000
．
1 ≦ X
i
≦ 1000000000
(
1 ≦ i ≦ N
)．
X
i
≠ X
j
(
1 ≦ i < j ≦ N
)．
1 ≦ V
i
≦ 1000000000
(
1 ≦ i ≦ N
)．
入力される値はすべて整数である．
入力
入力は以下の形式で標準入力から与えられる．
N
M
D
X
1
V
1
X
2
V
2
:
X
N
V
N
出力
条件を満たすような絵の残し方が存在しない場合，標準出力に
-1
を
1
行で出力せよ．
条件を満たすような絵の残し方が存在する場合，標準出力に，「エゴイ展」の華やかさの最大値を
1
行で出力せよ．
入出力例
入力例 1
3 1 34
10 250
30 200
50 500
出力例 1
500
絵
3
のみを残した場合，「エゴイ展」の華やかさは
500
となる．華やかさを
500
より大きくすることはできないため，
500
を出力する．
入力例 2
4 4 10
21 160
32 270
11 115
44 205
出力例 2
115
すべての絵を残すことができ，「エゴイ展」の華やかさは
115
となる．
入力例 3
4 4 14
21 160
32 270
11 115
44 205
出力例 3
-1
どの
2
つの絵の間も
14
メートル以上離れているように絵を残すことはできない．したがって，
－1
を出力する．
入力例 4
6 3 4
4 2
5 2
2 1
9 2
1 1
7 2
出力例 4
1
入力例 5
15 6 129
185 2821
683 3312
101 3406
485 2120
671 1992
869 2555
872 3123
237 2970
351 2374
996 2090
729 2686
375 2219
820 3085
511 3217
924 4229
出力例 5
2219
情報オリンピック日本委員会
作 『日本情報オリンピック 第1回 女性部門』

Submitted solutions:

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

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);
   
    ll N,M,D;
    cin>>N>>M>>D;

    vector<pair<ll,ll>> P(N);
    rep(i,N)cin>>P[i].first>>P[i].second;
    sort(all(P));
    ll L=-1,R=1e18;
    while(R-L>1){
        ll mid=(R+L)/2,cnt=0,x=-1e18;
        rep(i,N){
            if(P[i].second>=mid){
                if(P[i].first>=x+D){
                    cnt++;
                    x=P[i].first;
                }
            }
        }
        (cnt>=M?L:R)=mid;
    }
    cout<<L<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4676
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Painting {
    int x, v;
};

// 自定義比較函數
bool compare(const Painting& a, const Painting& b) {
    return a.x < b.x;
}

bool can_select_paintings(const vector<Painting>& paintings, int M, int D, int min_value) {
    int count = 0;
    int last_position = -D - 1;
    
    for (size_t i = 0; i < paintings.size(); ++i) {
        if (paintings[i].v >= min_value && paintings[i].x - last_position >= D) {
            count++;
            last_position = paintings[i].x;
        }
        if (count >= M) {
            return true;
        }
    }
    return false;
}

int main() {
    int N, M, D;
    cin >> N >> M >> D;

    vector<Painting> paintings(N);
    for (int i = 0; i < N; ++i) {
        cin >> paintings[i].x >> paintings[i].v;
    }

    // 根據位置進行排序
    sort(paintings.begin(), paintings.end(), compare);

    // 二分搜索
    int left = 1, right = 1000000000;
    int result = -1;
    
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (can_select_paintings(paintings, M, D, mid)) {
            result = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    cout << result << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3600
final_rank: 2

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 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 YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N, M, D, X, V;
  cin >> N >> M >> D;
  vc<pi> XV(N);
  rep(i, N) {
    cin >> X >> V;
    XV[i] = mp(X, V);
  }
  sort(all(XV));
  auto check = [&](ll v) -> bool {
    if (v <= 0) return true;
    ll last = -inf;
    ll cnt = 0;
    rep(i, N) {
      if ((XV[i].fi-last) < D) continue;
      if (XV[i].se < v) continue;
      last = XV[i].fi;
      cnt++;
    }
    return cnt >= M;
  };
  ll ans = -1;
  ll left=1, right=(1<<30);
  rep(33) {
    ll m = (left+right) / 2;
    if (check(m)) {
      chmax(ans, m);
      left = m;
    } else {
      right = m;
    }
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4668
final_rank: 9

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/algorithm/compress.hpp"
#include <algorithm>
#include <iterator>
#include <vector>

/**
 * @brief 座標圧縮
 *
 * @tparam T 要素の型
 */
template <class T>
struct coordinate_compression {
    coordinate_compression() = default;
    coordinate_compression(const std::vector<T> &_data) : data(_data) { build(); }

    const T &operator[](int i) const { return data[i]; }
    T &operator[](int i) { return data[i]; }

    void add(T x) { data.emplace_back(x); }

    void build() {
        std::sort(std::begin(data), std::end(data));
        data.erase(std::unique(std::begin(data), std::end(data)), std::end(data));
    }

    bool exists(T x) const {
        auto it = std::lower_bound(std::begin(data), std::end(data), x);
        return it != std::end(data) && *it == x;
    }

    int get(T x) const {
        auto it = std::lower_bound(std::begin(data), std::end(data), x);
        return std::distance(std::begin(data), it);
    }

    int size() const { return std::size(data); }

  private:
    std::vector<T> data;
};

/**
 * @brief 座標圧縮
 *
 * @tparam T 要素の型
 * @param v 配列
 * @return std::vector<T>
 */
template <class T>
std::vector<T> compress(const std::vector<T> &v) {
    coordinate_compression cps(v);
    std::vector<T> res;
    res.reserve(std::size(v));
    for (auto &&x : v) res.emplace_back(cps.get(x));
    return res;
}
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 4 "a.cpp"

int main(void) {
    int n, m, d;
    cin >> n >> m >> d;
    vector<pair<int, int>> a(n);
    cin >> a;
    sort(all(a));

    vector<int> v(n);
    rep (i, n) v[i] = a[i].second;
    coordinate_compression cps(v);

    int l = -1, r = cps.size();
    while (r - l > 1) {
        int mid = (l + r) / 2;
        int x = -Inf;
        int s = 0;
        rep (i, n) {
            if (x + d <= a[i].first && cps[mid] <= a[i].second) {
                ++s;
                x = a[i].first;
            }
        }
        if (s >= m)
            l = mid;
        else
            r = mid;
    }
    co(l == -1 ? -1 : cps[l]);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4668
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL<<40;

int main() {
    long long N, M, D;
    cin >> N >> M >> D;
    vector<long long> X(N), V(N);
    for (int i = 0; i < N; ++i) cin >> X[i] >> V[i];

    // 一直線上に左から順に並び替える (ここでは id を並び替える)
    vector<int> ids(N);
    iota(ids.begin(), ids.end(), 0);
    sort(ids.begin(), ids.end(), [&](int i, int j) {
        return X[i] < X[j];
    });

    // 二分探索
    long long low = -1, high = INF;
    while (high - low > 1) {
        long long x = (low + high) / 2;

        int con = 0;  // 選んだ枚数
        long long prev = -INF;  // 最後に選んだ絵の位置
        for (auto id : ids) {
            if (X[id] - prev >= D && V[id] >= x) {
                ++con; 
                prev = X[id];
            }
        }
        if (con >= M) low = x;
        else high = x;
    }
    cout << low << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 4940
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
int main(){
    int n, m, d, x, v;
    vector<int> vec;
    vector<Pi> p;
    cin >> n >> m >> d;
    for(int i=0;i<n;i++){
        cin >> x >> v;
        vec.push_back(v);
        p.push_back(Pi(x, v));
    }
    vec.push_back(-1);
    sort(vec.begin(), vec.end());
    sort(p.begin(), p.end());
    int mid, l=0, r=vec.size()-1;
    while(l<r){
        int sum=0, id=0;
        mid=(l+r+1)/2;
        for(int i=0;i<n;i++){
            if(p[i].second>=vec[mid] && (sum==0 || p[i].first-p[id].first>=d)){
                id = i;
                sum++;
            }
        }
        if(sum>=m) l = mid;
        else r = mid-1;
    }
    cout << vec[l] << endl;
    return(0);
}
accuracy: 1
time_ms: 70
memory_kb: 4280
final_rank: 8

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

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;

int N,M,D;
vector<P> d;
bool check(int x){
	int j=-1,k;
	for(k=1;k<=N;k++){
		if(d[k].second>=x){
			j=k;
			break;
		}
	}

	if(j==-1) return false;

	ll c=1;
	for(k=j+1;k<=N;k++){
		if(d[k].second>=x && d[k].first-d[j].first>=D){
			c++;
			j=k;
		}
	}

	if(c>=M) return true;
	else return false;
}

int main(void){
	int i;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	cin >> N >> M >> D;

	vector<int> v(N+1,-1);
	d.push_back(P(0,0));
	for(i=1;i<=N;i++){
		int x,y;
		cin >> x >> y;
		d.push_back(P(x,y));
		v[i]=y;
	}

	sort(d.begin(),d.end());
	sort(v.begin(),v.end());

	int ok=0,ng=N+1;
	while(ng-ok>1){
		int m=(ok+ng)/2;
		if(check(v[m])) ok=m;
		else ng=m;
	}


	cout << v[ok] << endl;


	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4492
final_rank: 6

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<random>
#include<bitset>
#include<cassert>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int main(){
	static int n,m,d,x[100000],v[100000];
	cin>>n>>m>>d;
	for(int i=0;i<n;i++)
		cin>>x[i]>>v[i];
	static pair<int,int>xv[100000];
	for(int i=0;i<n;i++)
		xv[i]={x[i],v[i]};
	sort(xv,xv+n);
	for(int i=0;i<n;i++)
		tie(x[i],v[i])=xv[i];
	int ok=-1,ng=1e9+7;
	while(ng-ok>1){
		int mid=(ng+ok)/2;
		int tmp=0,cnt=0;
		for(int i=0;i<n;i++){
			if(mid<=v[i]&&tmp<=x[i]){
				tmp=x[i]+d;
				cnt++;
			}
		}
		(cnt>=m?ok:ng)=mid;
	}
	cout<<ok<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4984
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool f(const vector<pair<int, int>>& XY, int V, int D, int M)
{
	for (int i = 0, j = 0, X = -1; j < (int) XY.size(); ++j) {
		if (XY[j].second < V) continue;
		if (X >= 0 && XY[j].first - X < D) continue;
		X = XY[j].first;
		if (++i >= M) return true;
	}
	return false;
}

int main()
{
	int N, M, D, V = 0; cin >> N >> M >> D;
	vector<pair<int, int>> XV(N); for (auto& [X, V] : XV) cin >> X >> V;
	sort(XV.begin(), XV.end());
	for (int d = 1 << 29; d > 0; d /= 2) if (f(XV, V + d, D, M)) V += d;
	cout << (V == 0 ? -1 : V) << endl;

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3896
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll n,m,d; cin >> n >> m >> d;
    vector<pair<int,int>> v(n);
    for(int i=0;i<n;i++){
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    ll L=-1,R=2e9;
    while(R-L>1){
        ll mid=(L+R)/2;
        ll last=-2e9;
        int cnt=0;
        for(int i=0;i<n;i++){
            if(v[i].second>=mid and v[i].first>=last+d){
                last=v[i].first;
                cnt++;
            }
        }
        if(cnt>=m){
            L=mid;
        }
        else{
            R=mid;
        }
    }
    cout << L << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3892
final_rank: 1

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int inf = 1000000001;
int n = -1;
int d = -1;
vector<pii> xv_vec;
vector<int> dif_vec;
map<piii, int> mem_map;


int solve(int i, int j, int m) {
    piii mem_key = piii(pii(i, j), m);
    int num_v = 0;
    int min_dif = inf;
    int max_min_v = -1;
    int max_m0 = 0;
    int min_v = inf;
    int min_v0 = inf;
    int min_v1 = inf;
    int k1 = 0;

    if (mem_map.count(mem_key) > 0) {
        return mem_map[mem_key];
    }
    
    num_v = j - i;
    if (num_v < m) {
        return -1;
    }

    if (m == 1) {
        int max_v = 0;

        for (int k = i; k < j; k++) {
            max_v = max(max_v, xv_vec[k].second);
        }
        mem_map[mem_key] = max_v;
        return max_v;
    }

    if (num_v == 1) {
        return -1;
    }

    if (num_v == 2) {
        if (m == 2) {
            if (dif_vec[i] >= d) {
                mem_map[mem_key] = min(xv_vec[i].second, xv_vec[i + 1].second);
                return mem_map[mem_key];
            }
            else {
                return -1;
            }
        }
        else {
            return -1;
        }
    }

    for (int k = i; k < j - 1; k++) {
        min_dif = min(min_dif, dif_vec[k]);
    }

    if (num_v == m) {
        if (min_dif >= d) {
            int min_v = inf;

            for (int k = i; k < j; k++) {
                min_v = min(min_v, xv_vec[k].second);
                mem_map[mem_key] = min_v;
                return min_v;
            }
        }
        else {
            return -1;
        }
    }
    
    for (int k0 = i + 1; k0 < j; k0++) {
        vector<int> m0_vec;

        max_m0 = min(m, min(k0 - i, j - k0) + 1);
        for (int l = 0; l < max_m0; l++) {
            m0_vec.push_back(l);
        }

        for (int& m0 : m0_vec) {
            min_v0 = solve(i, k0, m0);
            k1 = k0;
            min_v1 = -1;

            while (k1 < j && xv_vec[k1].first - xv_vec[k0 - 1].first < d) {
                k1++;
            }

            if (k1 < j) {
                min_v1 = solve(k1, j, m - m0);
            }

            if (min_v0 >= 0 && min_v1 >= 0) {
                min_v = min(min_v0, min_v1);
            }
            else {
                continue;
            }
            
            max_min_v = max(max_min_v, min_v);
        }
    }
    
    mem_map[mem_key] = max_min_v;
    return max_min_v;
}

int main(int argc, char **argv){
#if READ_FROM_FILE
    std::ifstream ifs("test1.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
    int m = -1;
    int max_v = 0;
    int x = 0;
    int v = 0;
    int dif = 0;
    
    cin >> n >> m >> d;

    for (int i = 0; i < n; i++) {
        cin >> x >> v;
        max_v = max(max_v, v);
        xv_vec.push_back(pii(x, v));
    }

    sort(xv_vec.begin(), xv_vec.end());
    for (int i = 1; i < n; i++) {
        dif = xv_vec[i].first - xv_vec[i - 1].first;
        dif_vec.push_back(dif);
    }

    if (m == 1) {
        cout << max_v << endl;
    }
    else {
        int total_d = xv_vec[n - 1].first - xv_vec[0].first;

        if (total_d < (m - 1) * d) {
            cout << -1 << endl;
        }
        else {
            cout << solve(0, n, m) << endl;
        }
    }

    return 0;
}
accuracy: 0.11940298507462686
time_ms: 50
memory_kb: 4684
final_rank: 16

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <fstream>

int main() {
	int n, m, d; std::cin >> n >> m >> d;
	std::vector<std::pair<int, int>> picture(n);
	for (auto& [x, v] : picture) {
		std::cin >> x >> v;
	}
	std::sort(picture.begin(), picture.end(), [](const auto a, const auto b) {return a.first < b.first; });
	int min = 0;
	int max = 1000000001;
	while (min < max) {
		const auto mid = (min + max) >> 1;
		int use{ 0 };
		int last{ -d };
		for (const auto [x, v] : picture) {
			if (mid <= v && last + d <= x) {
				++use;
				last = x;
			}
		}
		if (use >= m) {
			min = mid + 1;
		}
		else {
			max = mid;
		}
	}
	std::cout << max - 1 << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 3900
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>    
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<ctime>
#include<fstream>
#include<functional>
#include<map>
#include<math.h>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<tuple>
#include<vector>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define rep3(i, x, y) for(ll i = x; i < y; i++)
#define rep4(i, x) for(ll i = x - 1; i >= 0; i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;
typedef string::const_iterator State;
class ParseError {};

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	return gcd(b, a % b);
}
//ax + by = cが整数解をもつための必要十分条件は c が gcd(a,b) で割り切れること。
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res++;
		//res += n % 10;

		n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y, ll m) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= m; }
		x = x * x % m; y /= 2;
	}
	return res;
}

const int nCkMAX = 500000;
ll fac[nCkMAX], finv[nCkMAX], inv[nCkMAX];//n!、1/n!、1/n
// テーブルを作る前処理
void nCkinit(ll m) {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < nCkMAX; i++) {
		fac[i] = fac[i - 1] * i % m;
		inv[i] = m - inv[m % i] * (m / i) % m;
		finv[i] = finv[i - 1] * inv[i] % m;
	}
}
// 二項係数計算
ll nCk(ll n, ll k, ll m) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % m) % m;
}
//nが大きくてkが小さい時
//nCkMAXはk+2まででOK
ll nCk2(ll n, ll k, ll m) {
	ll res = 1;
	for (int i = n - k + 1; i <= n; i++) {
		res *= i; res %= m;
	}
	res = res * finv[k] % m;
	return res;
}

//xorshift　from 「ビット演算 (bit 演算) の使い方を総特集！ 」
//1~6まで出したいときは
//xorshift() % 6 + 1
unsigned int xorshift() {
	static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
	unsigned int tt = (tx ^ (tx << 11));
	tx = ty; ty = tz; tz = tw;
	return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}


//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <ld, ll> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };


vector<ll> spf;
void smallest_prime_factors(ll n) {
	spf.resize(n + 1); spf[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (!spf[i]) {
			for (int j = i; j < n; j += i) { spf[j] = i; }
		}
	}
}
std::map<int, int> high_speed_prime_factor(int n) {
	std::map<int, int> res;
	while (n != 1) {
		res[spf[n]]++;
		n /= spf[n];
	}
	return res;
}

////////////////////////////////struct
struct edge {
	ll to; ld cost;
};
struct Point {
	ll x, y;
};
bool operator<(const Point& a1, const Point& a2) {
	if (a1.x < a2.x) return true;
	if (a1.x > a2.x) return false;
	if (a1.y < a2.y) return true;
	return false;
}
struct status {
	ll x;
	ll v;

	//bool operator<(const status& rhs) const { return c < rhs.c; };
	//bool operator>(const status& rhs) const { return c > rhs.c; };
	bool operator<(const status& rhs) const { return x < rhs.x; };
	bool operator>(const status& rhs) const { return x > rhs.x; };
};
void yesno(bool ok) {
	if (ok) { cout << "Yes\n"; }
	else { cout << "No\n"; }
}
//多倍長整数演算
//nCkを求めるときにnCkMAXとnCkinit



int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	ll n, m, d; cin >> n >> m >> d;
	vector<status> pn(n);//painting
	rep(i, n) {
		cin >> pn[i].x >> pn[i].v;
	}
	sort(all(pn));

	//価値の最小値の最大化
	ll ok = -1, ng = 1000000001, mid = (ok + ng) / 2;
	while (ng - ok > 1) {
		ll cnt = 0, mae = 0 - d - 1;
		rep(i, n) {
			if (pn[i].x - mae >= d && pn[i].v >= mid) {
				cnt++; mae = pn[i].x;
			}
		}
		if (cnt >= m) { ok = mid; }
		else { ng = mid; }
		mid = (ok + ng) / 2;
	}
	cout << ok << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4676
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n,m,d);
    vector<array<int,2>> x(n);
    rep(i,n)in(x[i][0],x[i][1]);
    in(x);
    sort(all(x));
    int l=0,r=INF,mid;
    while(l<r){
        mid=(l+r)/2;
        int pre=-INF,cnt=0;
        rep(j,n){
            if(x[j][0]>=pre+d&&x[j][1]>=mid){
                cnt++;
                pre=x[j][0];
            }
        }
        if(cnt>=m)l=mid+1;
        else r=mid;
    }
    out(--l);
}
accuracy: 1
time_ms: 30
memory_kb: 3936
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001




#define SIZE 200005

struct Info{
	bool operator<(const struct Info& arg) const{

		return x < arg.x;
	}
	ll value,x;
};

enum Type{
	L,
	R,
};

int f_N,N;
ll table[2][6*SIZE];
ll M,D;
Info info[SIZE],work[SIZE];

void init(int first_N){
	N = 1;
	while(N < first_N)N *= 2;
}

void update(Type type,int loc,ll value){
	loc += N-1;

	table[type][loc] = value;

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		table[type][parent] = max(table[type][2*parent+1],table[type][2*parent+2]);

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


ll query(Type type,int search_left,int search_right,int node_id,int node_left,int node_right){

	//今回のノードが検索区間をカバーしていなければ、結果に関係ない値を返す
	if(search_right < node_left || search_left > node_right){

		return -HUGE_NUM;
	}

	//今回のノードの区間が、検索区間の部分区間である場合、今回のノードの値を返す
	if(search_left <= node_left && search_right >= node_right){
		return table[type][node_id];
	}

	//今回のノードの区間に、一部検索区間と重なっている区間がある場合→再帰的に子どもに尋ねる
	ll left_max = query(type,search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
	ll right_max = query(type,search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);

	return max(left_max,right_max);
}


bool isOK(ll mini){ //■最小値を決め打つ

	ll ind = 0;
	for(int i = 0; i < f_N; i++){

		if(info[i].value < mini)continue;

		work[ind] = info[i];
		ind++;
	}

	if(ind < M){

		return false;
	}

	init(ind);

	//0～N-2が上位構造のアドレス、N-1～2*N-2が最小要素のアドレス
	for(int i = 0; i <= 2*N-2; i++){

		table[L][i] = -HUGE_NUM;
		table[R][i] = -HUGE_NUM;
	}

	//■右方の、最大のパス長を求める
	update(R,ind-1,0);

	for(int i = ind-2; i >= 0; i--){

		int left = i,right = f_N-1,mid = (left+right)/2;
		int next = i;

		while(left <= right){

			if(work[mid].x >= work[i].x+D){
				next = mid;
				right = mid-1;
			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}

		if(next == i){
			update(R,i,0);
			continue;
		}

		ll tmp = query(R,next,ind-1,0,0,N-1);

		update(R,i,tmp+1);
	}

	//■左方の最大のパス長を求める
	update(L,0,0);

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

		int left = 0,right = i,mid = (left+right)/2;
		int next = i;

		while(left <= right){

			if(work[mid].x <= work[i].x-D){

				next = mid;
				left = mid+1;

			}else{

				right = mid-1;
			}
			mid = (left+right)/2;
		}

		if(next == i){

			update(L,i,0);
			continue;
		}

		ll tmp = query(L,0,next,0,0,N-1);

		update(L,i,tmp+1);
	}

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

		ll to_L = query(L,i,i,0,0,N-1);
		ll to_R = query(R,i,i,0,0,N-1);

		if(to_L+to_R+1 >= M){

			return true;
		}
	}

	return false;
}


int main(){

	scanf("%d %lld %lld",&f_N,&M,&D);

	vector<ll> vec;

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

		scanf("%lld %lld",&info[i].x,&info[i].value);
		vec.push_back(info[i].value);
	}
	sort(info,info+f_N);

	sort(vec.begin(),vec.end());
	vec.erase(unique(vec.begin(),vec.end()),vec.end());

	int left = 0,right = vec.size()-1,mid = (left+right)/2;
	int ind = -1; //なるべく大きく

	while(left <= right){
		if(isOK(vec[mid])){

			ind = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	if(ind == -1){

		printf("-1\n");
	}else{

		printf("%lld\n",vec[ind]);
	}

	return 0;
}
accuracy: 1
time_ms: 950
memory_kb: 11032
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001




#define SIZE 200005

struct Info{
	bool operator<(const struct Info& arg) const{

		return x < arg.x;
	}
	ll value,x;
};

enum Type{
	L,
	R,
};

int f_N,N;
ll table[2][6*SIZE];
ll M,D;
Info info[SIZE],work[SIZE];

void init(int first_N){
	N = 1;
	while(N < first_N)N *= 2;
}

void update(Type type,int loc,ll value){
	loc += N-1;

	table[type][loc] = value;

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		table[type][parent] = max(table[type][2*parent+1],table[type][2*parent+2]);

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


ll query(Type type,int search_left,int search_right,int node_id,int node_left,int node_right){

	//今回のノードが検索区間をカバーしていなければ、結果に関係ない値を返す
	if(search_right < node_left || search_left > node_right){

		return -HUGE_NUM;
	}

	//今回のノードの区間が、検索区間の部分区間である場合、今回のノードの値を返す
	if(search_left <= node_left && search_right >= node_right){
		return table[type][node_id];
	}

	//今回のノードの区間に、一部検索区間と重なっている区間がある場合→再帰的に子どもに尋ねる
	ll left_max = query(type,search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
	ll right_max = query(type,search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);

	return max(left_max,right_max);
}


bool isOK(ll mini){ //■最小値を決め打つ

	ll ind = 0;
	for(int i = 0; i < f_N; i++){

		if(info[i].value < mini)continue;

		work[ind] = info[i];
		ind++;
	}

	if(ind < M){

		return false;
	}

	init(ind);

	//0～N-2が上位構造のアドレス、N-1～2*N-2が最小要素のアドレス
	for(int i = 0; i <= 2*N-2; i++){

		table[L][i] = -HUGE_NUM;
		table[R][i] = -HUGE_NUM;
	}

	//■右方の、最大のパス長を求める
	update(R,ind-1,0);

	for(int i = ind-2; i >= 0; i--){

		int left = i,right = f_N-1,mid = (left+right)/2;
		int next = i;

		while(left <= right){

			if(work[mid].x >= work[i].x+D){
				next = mid;
				right = mid-1;
			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}

		if(next == i){
			update(R,i,0);
			continue;
		}

		ll tmp = query(R,next,ind-1,0,0,N-1);

		//printf("\ni;%d tmp:%lld next:%d\n",i,tmp,next);

		update(R,i,tmp+1);
	}

	//■左方の最大のパス長を求める
	update(L,0,0);

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

		int left = 0,right = i,mid = (left+right)/2;
		int next = i;

		while(left <= right){

			if(work[mid].x <= work[i].x-D){

				next = mid;
				left = mid+1;

			}else{

				right = mid-1;
			}
			mid = (left+right)/2;
		}

		if(next == i){

			update(L,i,0);
			continue;
		}

		ll tmp = query(L,0,next,0,0,N-1);

		//printf("\ni;%d tmp:%lld next:%d\n",i,tmp,next);

		update(L,i,tmp+1);
	}

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

		ll to_L = query(L,i,i,0,0,N-1);
		ll to_R = query(R,i,i,0,0,N-1);

	//	printf("\ni:%d to_L:%lld to_R:%lld\n",i,to_L,to_R);

		if(to_L+to_R+1 >= M){

			return true;
		}
	}

	return false;
}


int main(){

	scanf("%d %lld %lld",&f_N,&M,&D);

	vector<ll> vec;

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

		scanf("%lld %lld",&info[i].x,&info[i].value);
		vec.push_back(info[i].value);
	}
	sort(info,info+f_N);

	sort(vec.begin(),vec.end());
	vec.erase(unique(vec.begin(),vec.end()),vec.end());

	int left = 0,right = vec.size()-1,mid = (left+right)/2;
	int ind = -1; //なるべく大きく

	while(left <= right){
		if(isOK(vec[mid])){

			ind = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	if(ind == -1){

		printf("-1\n");
	}else{

		printf("%lld\n",vec[ind]);
	}

	return 0;
}
accuracy: 1
time_ms: 950
memory_kb: 11068
final_rank: 15