Problem p03038 (C++)

Buggy submission (status: Runtime Error):
//==========================Head files==========================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define db double
#define mp make_pair
#define pr pair<int, int>
#define fir first
#define sec second
#define pb push_back
#define ms(i, j) memset(i, j, sizeof i)
using namespace std;
//==========================Templates==========================
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
inline LL readl() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    b >>= 1;
    a = a * a;
  }
  return ans;
}
int power_mod(int a, int b, int mod) {
  a %= mod;
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    b >>= 1, a = (a * a) % mod;
  }
  return ans;
}
LL powerl(LL a, LL b) {
  LL ans = 1ll;
  while (b) {
    if (b & 1ll)
      ans = ans * a;
    b >>= 1ll;
    a = a * a;
  }
  return ans;
}
LL power_modl(LL a, LL b, LL mod) {
  a %= mod;
  LL ans = 1ll;
  while (b) {
    if (b & 1ll)
      ans = (ans * a) % mod;
    b >>= 1ll, a = (a * a) % mod;
  }
  return ans;
}
LL gcdl(LL a, LL b) { return b == 0 ? a : gcdl(b, a % b); }
LL abssl(LL a) { return a > 0 ? a : -a; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int abss(int a) { return a > 0 ? a : -a; }
//==========================Main body==========================
#define LD "%I64d"
#define D "%d"
#define pt printf
#define sn scanf
#define pty printf("YES\n")
#define ptn printf("NO\n")
//==========================Code here==========================
int n, m, a[100000 + 5];
pair<int, int> p[100000 + 5];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d", &p[i].second, &p[i].first), p[i].second = -p[i].second,
                                              p[i].first = -p[i].first;
  sort(p + 1, p + 1 + m);
  int now = 1;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= -p[i].second; ++j) {
      if (-p[i].first < a[now])
        break;
      a[now] = -p[i].first;
      ++now;
    }
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += a[i];
  cout << ans;
  return 0;
}

Fixed submission:
//==========================Head files==========================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define db double
#define mp make_pair
#define pr pair<int, int>
#define fir first
#define sec second
#define pb push_back
#define ms(i, j) memset(i, j, sizeof i)
using namespace std;
//==========================Templates==========================
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
inline LL readl() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    b >>= 1;
    a = a * a;
  }
  return ans;
}
int power_mod(int a, int b, int mod) {
  a %= mod;
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    b >>= 1, a = (a * a) % mod;
  }
  return ans;
}
LL powerl(LL a, LL b) {
  LL ans = 1ll;
  while (b) {
    if (b & 1ll)
      ans = ans * a;
    b >>= 1ll;
    a = a * a;
  }
  return ans;
}
LL power_modl(LL a, LL b, LL mod) {
  a %= mod;
  LL ans = 1ll;
  while (b) {
    if (b & 1ll)
      ans = (ans * a) % mod;
    b >>= 1ll, a = (a * a) % mod;
  }
  return ans;
}
LL gcdl(LL a, LL b) { return b == 0 ? a : gcdl(b, a % b); }
LL abssl(LL a) { return a > 0 ? a : -a; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int abss(int a) { return a > 0 ? a : -a; }
//==========================Main body==========================
#define LD "%I64d"
#define D "%d"
#define pt printf
#define sn scanf
#define pty printf("YES\n")
#define ptn printf("NO\n")
//==========================Code here==========================
int n, m, a[100000 + 5];
pair<int, int> p[100000 + 5];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d", &p[i].second, &p[i].first), p[i].second = -p[i].second,
                                              p[i].first = -p[i].first;
  sort(p + 1, p + 1 + m);
  int now = 1;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= -p[i].second; ++j) {
      if (now > n)
        break;
      if (-p[i].first < a[now])
        break;
      a[now] = -p[i].first;
      ++now;
    }
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += a[i];
  cout << ans;
  return 0;
}