Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#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 INF 2e9
#define ALL(v) v.begin(), v.end()
int n, m;
ll a[100000];
vector<pair<ll, int>> b;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int t;
    ll t1;
    cin >> t >> t1;
    b.push_back(make_pair(t1, t));
  }
  sort(a, a + n);
  sort(b.begin(), b.end());
  int ch = 0;
  for (int i = m - 1; i >= 0; i--) {
    if (a[n - 1] < b[i].first && b[i].second + ch - 1 > n - 1) {
      for (int j = ch; j < n; j++) {
        a[j] = b[i].first;
      }
      break;
    }
    if (a[b[i].second + ch - 1] < b[i].first) {
      for (int j = ch; j < b[i].second + ch; j++) {
        a[j] = b[i].first;
      }
      ch = b[i].second + ch;
    } else {
      bool c = true;
      int k = ch;
      while (c && k < n) {
        if (a[k] < b[i].first) {
          a[k] = b[i].first;
          k++;
        } else {
          c = false;
        }
      }
      break;
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << "\n";
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#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 INF 2e9
#define ALL(v) v.begin(), v.end()
int n, m;
ll a[100000];
vector<pair<ll, int>> b;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int t;
    ll t1;
    cin >> t >> t1;
    b.push_back(make_pair(t1, t));
  }
  sort(a, a + n);
  sort(b.begin(), b.end());
  int ch = 0;
  for (int i = m - 1; i >= 0; i--) {
    if (a[n - 1] < b[i].first && b[i].second + ch - 1 > n - 1) {
      for (int j = ch; j < n; j++) {
        a[j] = b[i].first;
      }
      break;
    }
    if (a[min(n - 1, b[i].second + ch - 1)] < b[i].first) {
      for (int j = ch; j < min(n, b[i].second + ch); j++) {
        a[j] = b[i].first;
      }
      ch = b[i].second + ch;
    } else {
      bool c = true;
      int k = ch;
      while (c && k < n) {
        if (a[k] < b[i].first) {
          a[k] = b[i].first;
          k++;
        } else {
          c = false;
        }
      }
      break;
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << "\n";
}