Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cstring>
#include <queue>
#define mp make_pair
#define pb push_back
#define p push

#define FOR(a, c) for (int a = 0; a < c; a++)
#define ll long long int

#define getI(a) scanf("%d", &a)
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define getL(a) scanf("%lld", &a)
#define getLL(a, b) scanf("%lld%lld", &a, &b)
#define getLLL(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define BEND(c) (c).begin(), (c).end()
#define BEND2(c) (c).rbegin(), (c).rend()
#define fi first
#define se second
#define pi 3.1415926535897932384626
#define getS(a) scanf("%s", a)
#define pfi(a) printf("%d", a);
#define pfii(a, b) printf("%d%d", a, b);
#define pfl(a) printf("%lld", a);
#define pfll(a, b) printf("%lld%lld", a, b);
#define pfd(a) printf("%lf", a);
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);

#define sag (sol | 1)
#define sol (root << 1)
#define ort (bas + son) / 2
#define SIZE 1000000007
#define MAXN 100005

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> graph;

int main() {
  IOS ll n, m;
  cin >> n >> m;
  priority_queue<int, vector<int>, greater<int>> qq;
  for (int a, i = 0; i < n; ++i) {
    cin >> a;
    qq.push(a);
  }

  vpi vec2;

  for (int a, b, j = 0; j < m; ++j) {
    cin >> a >> b;
    vec2.pb({b, a});
  }

  sort(BEND(vec2));

  for (int l = vec2.size() - 1; l >= 0; --l) {
    int t = 10;

    while (t > 0) {

      if (vec2[l].first < qq.top())
        break;

      t = vec2[l].second;

      qq.pop();
      qq.push(vec2[l].first);
      if (vec2[l].second == 0) {
        break;
      }
      vec2[l].second--;
      t--;
    }

    if (vec2[l].first < qq.top())
      break;
  }
  ll sum = 0;
  for (int k = 0; k < n; ++k) {
    // cout << qq.top() <<" ";
    sum += qq.top();
    qq.pop();
  }
  cout << sum;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstring>
#include <queue>
#define mp make_pair
#define pb push_back
#define p push

#define FOR(a, c) for (int a = 0; a < c; a++)
#define ll long long int

#define getI(a) scanf("%d", &a)
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define getL(a) scanf("%lld", &a)
#define getLL(a, b) scanf("%lld%lld", &a, &b)
#define getLLL(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define BEND(c) (c).begin(), (c).end()
#define BEND2(c) (c).rbegin(), (c).rend()
#define fi first
#define se second
#define pi 3.1415926535897932384626
#define getS(a) scanf("%s", a)
#define pfi(a) printf("%d", a);
#define pfii(a, b) printf("%d%d", a, b);
#define pfl(a) printf("%lld", a);
#define pfll(a, b) printf("%lld%lld", a, b);
#define pfd(a) printf("%lf", a);
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);

#define sag (sol | 1)
#define sol (root << 1)
#define ort (bas + son) / 2
#define SIZE 1000000007
#define MAXN 100005

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> graph;

int main() {
  IOS ll n, m;
  cin >> n >> m;
  priority_queue<int, vector<int>, greater<int>> qq;
  for (int a, i = 0; i < n; ++i) {
    cin >> a;
    qq.push(a);
  }

  vpi vec2;

  for (int a, b, j = 0; j < m; ++j) {
    cin >> a >> b;
    vec2.pb({b, a});
  }

  sort(BEND(vec2));

  for (int l = vec2.size() - 1; l >= 0; --l) {
    int t = 10;

    while (t > 0) {

      if (vec2[l].first <= qq.top())
        break;

      t = vec2[l].second;

      qq.pop();
      qq.push(vec2[l].first);
      if (vec2[l].second == 0) {
        break;
      }
      vec2[l].second--;
      t--;
    }

    if (vec2[l].first < qq.top())
      break;
  }
  ll sum = 0;
  for (int k = 0; k < n; ++k) {
    // cout << qq.top() <<" ";
    sum += qq.top();
    qq.pop();
  }
  cout << sum;

  return 0;
}