Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define swap(a, b)                                                             \
  do {                                                                         \
    typeof(a) __tmp = (a);                                                     \
    (a) = (b);                                                                 \
    (b) = __tmp;                                                               \
  } while (0)
#define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i)
#define rep(i, n) repd(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YesNo(x) cout << ((x) ? "Yes" : "No") << endl
#define YESNO(x) cout << ((x) ? "YES" : "NO") << endl
#define absi(x) (((x) >= 0) ? (x) : (-(x)))

#define pr(v)                                                                  \
  for (auto x : v)                                                             \
    cerr << x << " ";                                                          \
  cerr << endl;

bool f(pair<ll, ll> a, pair<ll, ll> b) {
  return a.second == b.second ? a.first >= b.first : a.second > b.second;
}

int main(int argc, const char *argv[]) {
  ll n, m;
  cin >> n >> m;

  vector<ll> a(n);
  vector<pair<ll, ll>> bc(m);
  rep(i, n) { cin >> a.at(i); }
  rep(i, m) { cin >> bc[i].first >> bc[i].second; }
  sort(bc.begin(), bc.end(), f);

  vector<ll> d;
  rep(i, m) {
    vector<ll> tmp(bc[i].first, bc[i].second);
    d.insert(d.end(), tmp.begin(), tmp.end());
    if (n <= d.size())
      break;
  }

  d.insert(d.end(), a.begin(), a.end());
  sort(d.begin(), d.end(), greater<ll>());

  ll ret = 0;
  rep(i, n) { ret += d[i]; }

  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define swap(a, b)                                                             \
  do {                                                                         \
    typeof(a) __tmp = (a);                                                     \
    (a) = (b);                                                                 \
    (b) = __tmp;                                                               \
  } while (0)
#define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i)
#define rep(i, n) repd(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YesNo(x) cout << ((x) ? "Yes" : "No") << endl
#define YESNO(x) cout << ((x) ? "YES" : "NO") << endl
#define absi(x) (((x) >= 0) ? (x) : (-(x)))

#define pr(v)                                                                  \
  for (auto x : v)                                                             \
    cerr << x << " ";                                                          \
  cerr << endl;

bool f(pair<ll, ll> a, pair<ll, ll> b) { return (a.second > b.second); }

int main(int argc, const char *argv[]) {
  ll n, m;
  cin >> n >> m;

  vector<ll> a(n);
  vector<pair<ll, ll>> bc(m);
  rep(i, n) { cin >> a.at(i); }
  rep(i, m) { cin >> bc[i].first >> bc[i].second; }
  sort(bc.begin(), bc.end(), f);

  vector<ll> d;
  rep(i, m) {
    vector<ll> tmp(bc[i].first, bc[i].second);
    d.insert(d.end(), tmp.begin(), tmp.end());
    if (n <= d.size())
      break;
  }

  d.insert(d.end(), a.begin(), a.end());
  sort(d.begin(), d.end(), greater<ll>());

  ll ret = 0;
  rep(i, n) { ret += d[i]; }

  cout << ret << endl;
  return 0;
}