Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

ll modpow(ll n, ll k) {
  if (k == 1) {
    return n;
  } else if (k == 0) {
    return 1;
  } else if (k % 2 == 0) {
    ll memo = modpow(n, k / 2) % MOD;
    return memo * memo % MOD;
  } else {
    return n * modpow(n, k - 1) % MOD;
  }
}

int main() {

  ll n, m;
  cin >> n >> m;
  vector<ll> v;
  REP(i, n) {
    ll t;
    cin >> t;
    v.pb(t);
  }
  VSORT(v);

  vector<LP> vlp;
  REP(i, m) {
    ll b, c;
    cin >> b >> c;
    LP lp;
    lp.first = c;
    lp.second = b;
    vlp.pb(lp);
  }

  sort(vlp.begin(), vlp.end());

  ll vlpCounter = m - 1;
  ll vCounter = 0;
  bool bo = false;

  while (vlpCounter >= 0) {
    ll c = vlp[vlpCounter].first;
    ll b = vlp[vlpCounter].second;

    for (int i = vCounter; i < vCounter + b; i++) {
      if (v[i] <= c) {
        v[i] = c;
      } else {
        bo = true;
        break;
      }
    }
    if (bo)
      break;
    vCounter += b;
    vlpCounter--;
    /*vector<ll>::iterator it = lower_bound(v.begin(),v.end(),c);
    if(it==v.begin())break;

    ll num = it - v.begin();
    if(num<=b){
      REP(i,num){
        v[i] = c;
      }
      break;
    }else{
      for(int i = num-1;i>=num - b;i--){
        v[i] = c;
      }
    }
    vlpCounter--;

    REP(i,n){
      cout<<v[i]<<"    ";
    }
    cout<<endl;*/
  }

  ll ans = 0;
  REP(i, n) { ans += v[i]; }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

ll modpow(ll n, ll k) {
  if (k == 1) {
    return n;
  } else if (k == 0) {
    return 1;
  } else if (k % 2 == 0) {
    ll memo = modpow(n, k / 2) % MOD;
    return memo * memo % MOD;
  } else {
    return n * modpow(n, k - 1) % MOD;
  }
}

int main() {

  ll n, m;
  cin >> n >> m;
  vector<ll> v;
  REP(i, n) {
    ll t;
    cin >> t;
    v.pb(t);
  }
  VSORT(v);

  vector<LP> vlp;
  REP(i, m) {
    ll b, c;
    cin >> b >> c;
    LP lp;
    lp.first = c;
    lp.second = b;
    vlp.pb(lp);
  }

  sort(vlp.begin(), vlp.end());

  ll vlpCounter = m - 1;
  ll vCounter = 0;
  bool bo = false;

  while (vlpCounter >= 0) {
    ll c = vlp[vlpCounter].first;
    ll b = vlp[vlpCounter].second;

    for (int i = vCounter; i < vCounter + b; i++) {
      if (i == n) {
        bo = true;
        break;
      }
      if (v[i] <= c) {
        v[i] = c;
      } else {
        bo = true;
        break;
      }
    }
    if (bo)
      break;
    vCounter += b;
    vlpCounter--;
    /*vector<ll>::iterator it = lower_bound(v.begin(),v.end(),c);
    if(it==v.begin())break;

    ll num = it - v.begin();
    if(num<=b){
      REP(i,num){
        v[i] = c;
      }
      break;
    }else{
      for(int i = num-1;i>=num - b;i--){
        v[i] = c;
      }
    }
    vlpCounter--;

    REP(i,n){
      cout<<v[i]<<"    ";
    }
    cout<<endl;*/
  }

  ll ans = 0;
  REP(i, n) { ans += v[i]; }
  cout << ans << endl;

  return 0;
}