Problem p03721 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
using Graph = vector<vector<int>>;

using edge = struct {
  int to;
  ll cost;
};

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

typedef priority_queue<ll> PQ_HIGH;
typedef priority_queue<ll, VL, greater<ll>> PQ_LOW; // 昇順

// A 65
// a 97
// pairの比較関数(昇順)
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
// 降順
bool compare_by_b_high(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  PQ_LOW d;
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> data;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    data.push_back({a, b});
  }
  sort(data.begin(), data.end());
  int i = 0;
  while (k > 0) {
    k = k - data[i].second;
    i++;
  }
  i--;
  cout << data[i].first << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
using Graph = vector<vector<int>>;

using edge = struct {
  int to;
  ll cost;
};

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

typedef priority_queue<ll> PQ_HIGH;
typedef priority_queue<ll, VL, greater<ll>> PQ_LOW; // 昇順

// A 65
// a 97
// pairの比較関数(昇順)
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
// 降順
bool compare_by_b_high(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> data;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    data.push_back({a, b});
  }
  sort(data.begin(), data.end());
  int i = 0;
  while (k > 0) {
    k = k - data[i].second;
    i++;
  }
  i--;
  cout << data[i].first << endl;
}