Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define ri register int
using namespace std;
template <class T> inline void read(T &x) {
  x = 0;
  int ne = 0;
  char c;
  while (!isdigit(c = getchar()))
    ne = c == '-';
  x = c - 48;
  while (isdigit(c = getchar()))
    x = (x << 3) + (x << 1) + c - 48;
  x = ne ? -x : x;
  return;
}
const int maxn = 100005;
int N, m;
int a[maxn];
ll ans = 0;
struct Op {
  int v, cnt;
  bool operator<(const Op &x) const { return v < x.v; }
} opt[maxn];
struct Heap {
  int n, heap[maxn];
  inline void up(int fa) {
    while (fa > 1) {
      if (heap[fa] < heap[fa >> 1]) {
        swap(heap[fa], heap[fa >> 1]);
        fa = fa >> 1;
      } else
        break;
    }
  }
  inline void push(int v) {
    heap[++n] = v;
    up(n);
  }
  inline void down(int fa) {
    int s = fa << 1;
    while (s <= n) {
      if (s < n && heap[s] > heap[s + 1])
        s++;
      if (heap[s] < heap[fa]) {
        swap(heap[s], heap[fa]);
        fa = s;
        s = fa << 1;
      } else
        break;
    }
  }
  inline void pop() {
    heap[1] = heap[n--];
    down(1);
  }
  inline int top() { return heap[1]; }
} q;
int main() {
  int b, c;
  read(N), read(m);
  for (ri i = 1; i <= N; i++) {
    read(a[i]);
    q.push(a[i]);
    ans += a[i];
  }
  for (ri i = 1; i <= m; i++) {
    read(opt[i].cnt), read(opt[i].v);
  }
  sort(opt + 1, opt + 1 + m);
  // printf("%d\n",opt[1].v);
  for (ri k = 1; k <= m; k++) {
    b = opt[k].cnt, c = opt[k].v;
    bool flag = 0;
    for (ri i = 1; i <= b && !flag; i++) {
      if (q.top() >= c) {
        flag = 1;
      } else {
        ans -= q.top();
        q.pop();
        q.push(c);
        ans += c;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define ri register int
using namespace std;
template <class T> inline void read(T &x) {
  x = 0;
  int ne = 0;
  char c;
  while (!isdigit(c = getchar()))
    ne = c == '-';
  x = c - 48;
  while (isdigit(c = getchar()))
    x = (x << 3) + (x << 1) + c - 48;
  x = ne ? -x : x;
  return;
}
const int maxn = 100005;
int N, m;
int a[maxn];
ll ans = 0;
struct Op {
  int v, cnt;
  bool operator<(const Op &x) const { return v > x.v; }
} opt[maxn];
struct Heap {
  int n, heap[maxn];
  inline void up(int fa) {
    while (fa > 1) {
      if (heap[fa] < heap[fa >> 1]) {
        swap(heap[fa], heap[fa >> 1]);
        fa = fa >> 1;
      } else
        break;
    }
  }
  inline void push(int v) {
    heap[++n] = v;
    up(n);
  }
  inline void down(int fa) {
    int s = fa << 1;
    while (s <= n) {
      if (s < n && heap[s] > heap[s + 1])
        s++;
      if (heap[s] < heap[fa]) {
        swap(heap[s], heap[fa]);
        fa = s;
        s = fa << 1;
      } else
        break;
    }
  }
  inline void pop() {
    heap[1] = heap[n--];
    down(1);
  }
  inline int top() { return heap[1]; }
} q;
int main() {
  int b, c;
  read(N), read(m);
  for (ri i = 1; i <= N; i++) {
    read(a[i]);
    q.push(a[i]);
    ans += a[i];
  }
  for (ri i = 1; i <= m; i++) {
    read(opt[i].cnt), read(opt[i].v);
  }
  sort(opt + 1, opt + 1 + m);
  // printf("%d\n",opt[1].v);
  for (ri k = 1; k <= m; k++) {
    b = opt[k].cnt, c = opt[k].v;
    bool flag = 0;
    for (ri i = 1; i <= b && !flag; i++) {
      if (q.top() >= c) {
        flag = 1;
      } else {
        ans -= q.top();
        q.pop();
        q.push(c);
        ans += c;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}