Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // sort
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map> // pair
#include <queue>
#include <string>
#include <vector>

#define ABS(x) ((x) > 0 ? (x) : -(x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

using namespace std;
unsigned long long int newton(long long int a, long long int b) {
  if (a == b)
    return a;
  while (a != 0 && b != 0) {
    if (a > b) {
      b += a;
      a = b - a;
      b -= a;
    }
    b %= a;
  }
  return a;
}

struct UnionFind {
  vector<int> p;
  UnionFind(int N) : p(N) {
    for (int i = 0; i < N; i++) {
      p[i] = i;
    }
  }
  int root(int x) {
    if (p[x] == x)
      return x;
    return p[x] = root(p[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    p[rx] = ry;
  }
  bool issame(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  vector<int> A;
  // 値　個数
  priority_queue<pair<int, int>> q;
  cin >> N >> M;
  int i, j, k;
  for (i = 0; i < N; ++i) {
    cin >> j;
    A.push_back(j);
  }
  for (i = 0; i < M; ++i) {
    cin >> j >> k;
    q.push(make_pair(k, j));
  }
  sort(A.begin(), A.end());
  int cur = 0;
  int end = 0;
  while (!q.empty()) {
    j = q.top().first;
    k = q.top().second;
    q.pop();
    for (i = 0; i < k; ++i) {
      if (A[cur] < j) {
        A[cur] = j;
        ++cur;
      } else {
        end = 1;
        break;
      }
    }
    if (end)
      break;
  }
  unsigned long long int ans = 0;
  for (i = 0; i < N; ++i) {
    ans += A[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm> // sort
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map> // pair
#include <queue>
#include <string>
#include <vector>

#define ABS(x) ((x) > 0 ? (x) : -(x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

using namespace std;
unsigned long long int newton(long long int a, long long int b) {
  if (a == b)
    return a;
  while (a != 0 && b != 0) {
    if (a > b) {
      b += a;
      a = b - a;
      b -= a;
    }
    b %= a;
  }
  return a;
}

struct UnionFind {
  vector<int> p;
  UnionFind(int N) : p(N) {
    for (int i = 0; i < N; i++) {
      p[i] = i;
    }
  }
  int root(int x) {
    if (p[x] == x)
      return x;
    return p[x] = root(p[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    p[rx] = ry;
  }
  bool issame(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  vector<int> A;
  // 値　個数
  priority_queue<pair<int, int>> q;
  cin >> N >> M;
  int i, j, k;
  for (i = 0; i < N; ++i) {
    cin >> j;
    A.push_back(j);
  }
  for (i = 0; i < M; ++i) {
    cin >> j >> k;
    q.push(make_pair(k, j));
  }
  sort(A.begin(), A.end());
  int cur = 0;
  int end = 0;
  while (!q.empty()) {
    j = q.top().first;
    k = q.top().second;
    q.pop();
    for (i = 0; i < k; ++i) {
      if (A[cur] < j) {
        A[cur] = j;
        ++cur;
      } else {
        end = 1;
        break;
      }
      if (cur == N) {
        end = 1;
        break;
      }
    }
    if (end)
      break;
  }
  unsigned long long int ans = 0;
  for (i = 0; i < N; ++i) {
    ans += A[i];
  }
  cout << ans << endl;
  return 0;
}