Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2 + 1
#define LL_MAX 9223372036854775807         // ない環境用
#define LL_HALFMAX 9223372036854775807 / 2 // ない環境用
#define MIN -(9223372036854775807 / 2)
#define INF 9223372036854775807 / 2
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define mp make_pair

using namespace std;

std::mt19937 mt((int)time(0));

int dx[4] = {0, 1, 0, -1}; // x軸方向への変位
int dy[4] = {1, 0, -1, 0}; // y軸方向への変位

using Weight = int;
using Flow = int;
struct Edge {
  int src, dst;
  Weight weight;
  Flow cap;
  Edge() : src(0), dst(0), weight(0) {}
  Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
  g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
}

struct uf_tree {
  std::vector<int> parent;
  int __size;
  uf_tree(int size_) : parent(size_, -1), __size(size_) {}
  void unite(int x, int y) {
    if ((x = find(x)) != (y = find(y))) {
      if (parent[y] < parent[x])
        std::swap(x, y);
      parent[x] += parent[y];
      parent[y] = x;
      __size--;
    }
  }
  bool is_same(int x, int y) { return find(x) == find(y); }
  int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
  int size(int x) { return -parent[find(x)]; }
  int size() { return __size; }
};

//!!!問題をちゃんと読む!!!
//!!!問題をちゃんと読め!!!
//!!!問題は読みましたか？!!!

int imos[1000000];
int ans[1000000];
signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> A;
  rep(i, N) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());

  vector<pair<int, int>> CB;
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    CB.push_back(make_pair(c, b));
  }
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  int now = 0;
  for (auto cb : CB) {
    // bだけ置き換える
    rep(i, cb.second) {
      if (A[now] < cb.first) {
        A[now] = cb.first;
        now++;
      } else {
        break;
      }
    }
  }

  int ans = 0;
  rep(i, N) { ans += A[i]; }
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2 + 1
#define LL_MAX 9223372036854775807         // ない環境用
#define LL_HALFMAX 9223372036854775807 / 2 // ない環境用
#define MIN -(9223372036854775807 / 2)
#define INF 9223372036854775807 / 2
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define mp make_pair

using namespace std;

std::mt19937 mt((int)time(0));

int dx[4] = {0, 1, 0, -1}; // x軸方向への変位
int dy[4] = {1, 0, -1, 0}; // y軸方向への変位

using Weight = int;
using Flow = int;
struct Edge {
  int src, dst;
  Weight weight;
  Flow cap;
  Edge() : src(0), dst(0), weight(0) {}
  Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
  g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
}

struct uf_tree {
  std::vector<int> parent;
  int __size;
  uf_tree(int size_) : parent(size_, -1), __size(size_) {}
  void unite(int x, int y) {
    if ((x = find(x)) != (y = find(y))) {
      if (parent[y] < parent[x])
        std::swap(x, y);
      parent[x] += parent[y];
      parent[y] = x;
      __size--;
    }
  }
  bool is_same(int x, int y) { return find(x) == find(y); }
  int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
  int size(int x) { return -parent[find(x)]; }
  int size() { return __size; }
};

//!!!問題をちゃんと読む!!!
//!!!問題をちゃんと読め!!!
//!!!問題は読みましたか？!!!

int imos[1000000];
int ans[1000000];
signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> A;
  rep(i, N) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());

  vector<pair<int, int>> CB;
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    CB.push_back(make_pair(c, b));
  }
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  int now = 0;
  for (auto cb : CB) {
    // bだけ置き換える
    rep(i, cb.second) {
      if (A[now] < cb.first) {
        A[now] = cb.first;
        now++;
      } else {
        break;
      }
    }
    if (now >= N)
      break;
  }

  int ans = 0;
  rep(i, N) { ans += A[i]; }
  cout << ans << "\n";

  return 0;
}