Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#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 int long long
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919364364
#define all(vec) vec.begin(), vec.end()
#define ggr                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0;
#define ll long long
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 1)
    return 1;
  return kai(x - 1) * x % mod;
}

int mod_pow(int x, int y, int moder) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % moder;
    x = x * x % moder;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  if (x == y)
    return 1;
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}

int n, m, a[114514], b[114514], c[114514];
vector<pair<int, int>> ta;
int ans;

signed main() {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    cin >> b[i] >> c[i];
    ta.push_back({c[i], b[i]});
  }
  sort(all(ta));
  reverse(all(ta));
  int k = 0;
  rep(i, m) {
    for (int j = k; j < k + ta[i].second; j++) {
      if (j == n)
        break;
      if (a[j] < ta[i].first)
        a[j] = ta[i].first;
      else
        break;
    }
    k += ta[i].second;
  }
  rep(i, n) ans += a[i];
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#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 int long long
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919364364
#define all(vec) vec.begin(), vec.end()
#define ggr                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0;
#define ll long long
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 1)
    return 1;
  return kai(x - 1) * x % mod;
}

int mod_pow(int x, int y, int moder) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % moder;
    x = x * x % moder;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  if (x == y)
    return 1;
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}

int n, m, a[114514], b[114514], c[114514];
vector<pair<int, int>> ta;
int ans;

signed main() {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    cin >> b[i] >> c[i];
    ta.push_back({c[i], b[i]});
  }
  sort(all(ta));
  reverse(all(ta));
  int k = 0;
  rep(i, m) {
    for (int j = k; j < k + ta[i].second; j++) {
      if (j >= n)
        break;
      if (a[j] < ta[i].first)
        a[j] = ta[i].first;
      else
        break;
    }
    k += ta[i].second;
  }
  rep(i, n) ans += a[i];
  cout << ans << endl;
}