Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define mod 1000000007
#define ll long long int
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x, y)

using namespace std;

ll power(ll a, ll b, ll m) {
  if (b == 0)
    return (1);
  ll sol = power(a, b / 2, m);
  sol = (sol * sol) % m;
  if (b % 2 == 1)
    sol = (sol * a) % m;
  return (sol);
}
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else {
    return gcd(b, a % b);
  }
}
ll a[100001];
vector<pair<ll, ll>> v;
void solve() {
  ll sol = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    v.pb(MP(c, b));
  }
  sort(v.begin(), v.end());
  int i = 0, j = m - 1;
  ;
  while (1) {
    ll co = v[j].second;
    while (co > 0 && j >= 0) {
      ll val = v[j].first;
      if (a[i] < val) {
        a[i] = val;
        co--;
        i++;
      } else {
        for (int i = 0; i < n; i++) {
          sol = sol + a[i];
        }
        cout << sol;
        return;
      }
    }
    j--;
    if (j < 0) {
      for (int i = 0; i < n; i++) {
        sol = sol + a[i];
      }
      cout << sol;
      return;
    }
  }
}
int main() {
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return (0);
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define mod 1000000007
#define ll long long int
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x, y)

using namespace std;

ll power(ll a, ll b, ll m) {
  if (b == 0)
    return (1);
  ll sol = power(a, b / 2, m);
  sol = (sol * sol) % m;
  if (b % 2 == 1)
    sol = (sol * a) % m;
  return (sol);
}
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else {
    return gcd(b, a % b);
  }
}
ll a[100001];
vector<pair<ll, ll>> v;
void solve() {
  ll sol = 0;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    v.pb(MP(c, b));
  }
  sort(v.begin(), v.end());
  int i = 0, j = m - 1;
  ;
  while (1) {
    ll co = v[j].second;
    while (co > 0 && j >= 0) {
      ll val = v[j].first;
      if (a[i] < val) {
        a[i] = val;
        co--;
        i++;
        if (i >= n) {
          for (int i = 0; i < n; i++) {
            sol = sol + a[i];
          }
          cout << sol;
          return;
        }

      } else {
        for (int i = 0; i < n; i++) {
          sol = sol + a[i];
        }
        cout << sol;
        return;
      }
    }
    j--;
    if (j < 0) {
      for (int i = 0; i < n; i++) {
        sol = sol + a[i];
      }
      cout << sol;
      return;
    }
  }
}
int main() {
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return (0);
}