Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

vector<int> init(int size) {
  vector<int> par(size, -1);
  return par;
}

int root(int x, vector<int> &par) {
  if (par[x] < 0) {
    return x;
  } else {
    return par[x] = root(par[x], par);
  }
}

bool same(int x, int y, vector<int> &par) {
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int> &par) {
  x = root(x, par), y = root(y, par);
  if (same(x, y, par))
    return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int> &par) { return -par[root(x, par)]; }

int pow(int x, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x % MOD, n / 2);
  else
    return x * pow(x, n - 1) % MOD;
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> ac(1, 0);
  int ans = 0, bcsum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    ac.push_back(a);
    ans += a;
  }
  sort(ac.begin(), ac.end());
  rep(i, n) ac[i + 1] += ac[i];

  vector<pair<int, int>> ps;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    ps.push_back(make_pair(c, b));
  }
  sort(ps.begin(), ps.end(), greater<pair<int, int>>());
  int ind = 0;
  rep(i, m) {
    bool end = false;
    rep(j, ps[i].second) {
      ind++;
      bcsum += ps[i].first;
      // cout << bcsum << " " << ac[n] << " " << ac[ind] << " " << bcsum + ac[n]
      // - ac[ind] << endl;
      if (ans > bcsum + ac[n] - ac[ind]) {
        end = true;
        break;
      } else {
        ans = bcsum + ac[n] - ac[ind];
      }
    }
    if (end)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

vector<int> init(int size) {
  vector<int> par(size, -1);
  return par;
}

int root(int x, vector<int> &par) {
  if (par[x] < 0) {
    return x;
  } else {
    return par[x] = root(par[x], par);
  }
}

bool same(int x, int y, vector<int> &par) {
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int> &par) {
  x = root(x, par), y = root(y, par);
  if (same(x, y, par))
    return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int> &par) { return -par[root(x, par)]; }

int pow(int x, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x % MOD, n / 2);
  else
    return x * pow(x, n - 1) % MOD;
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> ac(1, 0);
  int ans = 0, bcsum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    ac.push_back(a);
    ans += a;
  }
  sort(ac.begin(), ac.end());
  rep(i, n) ac[i + 1] += ac[i];

  vector<pair<int, int>> ps;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    ps.push_back(make_pair(c, b));
  }
  sort(ps.begin(), ps.end(), greater<pair<int, int>>());
  int ind = 0;
  rep(i, m) {
    bool end = false;
    rep(j, ps[i].second) {
      ind++;
      if (ind > n) {
        end = true;
        break;
      }
      bcsum += ps[i].first;
      // cout << bcsum << " " << ac[n] << " " << ac[ind] << " " << bcsum + ac[n]
      // - ac[ind] << endl;
      if (ans > bcsum + ac[n] - ac[ind]) {
        end = true;
        break;
      } else {
        ans = bcsum + ac[n] - ac[ind];
      }
    }
    if (end)
      break;
  }
  cout << ans << endl;
}