Problem p03103 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a, i, j, v)                                                      \
  for (ll k = i; k <= j; k++) {                                                \
    a[k] = v;                                                                  \
  }

int main() {
  int n, m;
  cin >> n >> m;
  vector<tuple<ll, int>> v;
  REP(i, n) {
    ll a;
    int b;
    cin >> a >> b;
    v.push_back(make_tuple(a, b));
  }
  sort(v.begin(), v.end(),
       [](tuple<ll, int> const &lhs, tuple<ll, int> const &rhs) {
         if (get<0>(lhs) != get<0>(rhs))
           return get<0>(lhs) < get<0>(rhs);
         if (get<1>(lhs) != get<1>(rhs))
           return get<1>(lhs) > get<1>(rhs);
         return true;
       });
  ll cost = 0;
  REP(i, n) {
    cost += get<0>(v[i]) * min(get<1>(v[i]), m);
    m -= min(get<1>(v[i]), m);
    if (m == 0) {
      break;
    }
  }
  cout << cost << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a, i, j, v)                                                      \
  for (ll k = i; k <= j; k++) {                                                \
    a[k] = v;                                                                  \
  }

int main() {
  int n, m;
  cin >> n >> m;
  vector<tuple<ll, int>> v;
  REP(i, n) {
    ll a;
    int b;
    cin >> a >> b;
    v.push_back(make_tuple(a, b));
  }
  sort(ALL(v));
  // sort(
  //     v.begin(),
  //     v.end(),
  //     [](tuple<ll, int> const &lhs, tuple<ll, int> const &rhs) {
  //         if (get<0>(lhs) != get<0>(rhs)) return get<0>(lhs) < get<0>(rhs);
  //         if (get<1>(lhs) != get<1>(rhs)) return get<1>(lhs) > get<1>(rhs);
  //         return true;
  //     }
  // );
  ll cost = 0;
  REP(i, n) {
    cost += get<0>(v[i]) * min(get<1>(v[i]), m);
    m -= min(get<1>(v[i]), m);
    if (m == 0) {
      break;
    }
  }
  cout << cost << endl;
  return 0;
}