Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<PII> VP;

// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, m) RFOR(i, m, 0)

// container util
//------------------------------------------
#define pb(a) push_back(a)
#define fst first
#define snd second
#define SORT(V) sort((V).begin(), (V).end())
#define REV(V) reverse((V).begin(), (V).end())

// constant
//------------------------------------------
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// grobal variable
//------------------------------------------

// def function, class
//------------------------------------------

// main
//------------------------------------------
signed main() {
  int N, M;
  cin >> N >> M;

  VI A(N);
  VP CB(M);

  REP(i, N) cin >> A.at(i);
  REP(i, M) cin >> CB.at(i).snd >> CB.at(i).fst;

  SORT(A);
  SORT(CB);
  REV(CB);

  // REP(i, M) cout << CB.at(i).fst << " " << CB.at(i).snd << endl;

  int now = 0;

  REP(i, M) {
    int cnt = 0;
    REP(j, CB.at(i).snd) {
      if (N < now + j)
        break;
      if (CB.at(i).fst > A.at(now + j)) {
        A.at(now + j) = CB.at(i).fst;
        cnt++;
      } else {
        break;
      }
    }
    now += cnt;
  }

  int ans = 0;

  REP(i, N) ans += A.at(i);

  // REP(i, N) cout << A.at(i) << endl;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<PII> VP;

// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, m) RFOR(i, m, 0)

// container util
//------------------------------------------
#define pb(a) push_back(a)
#define fst first
#define snd second
#define SORT(V) sort((V).begin(), (V).end())
#define REV(V) reverse((V).begin(), (V).end())

// constant
//------------------------------------------
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// grobal variable
//------------------------------------------

// def function, class
//------------------------------------------

// main
//------------------------------------------
signed main() {
  int N, M;
  cin >> N >> M;

  VI A(N);
  VP CB(M);

  REP(i, N) cin >> A.at(i);
  REP(i, M) cin >> CB.at(i).snd >> CB.at(i).fst;

  SORT(A);
  SORT(CB);
  REV(CB);

  // REP(i, M) cout << CB.at(i).fst << " " << CB.at(i).snd << endl;

  int now = 0;

  REP(i, M) {
    int cnt = 0;
    REP(j, CB.at(i).snd) {
      if (N <= now + j)
        break;
      if (CB.at(i).fst > A.at(now + j)) {
        A.at(now + j) = CB.at(i).fst;
        cnt++;
      } else {
        break;
      }
    }
    now += cnt;
  }

  int ans = 0;

  REP(i, N) ans += A.at(i);

  // REP(i, N) cout << A.at(i) << endl;

  cout << ans << endl;

  return 0;
}