Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

// えびちゃんの make_vector (ここから)
template <typename Tp, size_t N>
std::vector<Tp>
make_vector_detail(std::vector<size_t> &sizes,
                   typename std::enable_if<(N == 1), const Tp &>::type x) {
  return std::vector<Tp>(sizes[0], x);
}
template <typename Tp, size_t N>
auto make_vector_detail(std::vector<size_t> &sizes,
                        typename std::enable_if<(N > 1), const Tp &>::type x) {
  size_t size = sizes[N - 1];
  sizes.pop_back();
  return std::vector<decltype(make_vector_detail<Tp, N - 1>(sizes, x))>(
      size, make_vector_detail<Tp, N - 1>(sizes, x));
}

template <typename Tp, size_t N>
auto make_vector(const size_t (&sizes)[N], const Tp &x) {
  std::vector<size_t> s(N);
  for (size_t i = 0; i < N; ++i)
    s[i] = sizes[N - i - 1];
  return make_vector_detail<Tp, N>(s, x);
}
// えびちゃんの make_vector (ここまで)

signed main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());

  vector<pair<int, int>> p(m);

  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    p[i].first = c;
    p[i].second = b;
  }

  sort(p.rbegin(), p.rend());
  for (int i = 0; i < m; i++)
    cerr << p[i].first << " " << p[i].second << endl;

  int idx = 0;

  for (int i = 0; i < m; i++) {
    if (idx == n)
      break;

    int c = p[i].first;
    int b = p[i].second;
    for (int j = 0; j < b; j++) {
      if (idx == n)
        break;
      if (a[idx] < c) {
        a[idx] = c;
        idx++;
      }
    }
  }

  for (int i = 0; i < n; i++)
    cerr << a[i] << " ";
  cerr << endl;

  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += a[i];
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

// えびちゃんの make_vector (ここから)
template <typename Tp, size_t N>
std::vector<Tp>
make_vector_detail(std::vector<size_t> &sizes,
                   typename std::enable_if<(N == 1), const Tp &>::type x) {
  return std::vector<Tp>(sizes[0], x);
}
template <typename Tp, size_t N>
auto make_vector_detail(std::vector<size_t> &sizes,
                        typename std::enable_if<(N > 1), const Tp &>::type x) {
  size_t size = sizes[N - 1];
  sizes.pop_back();
  return std::vector<decltype(make_vector_detail<Tp, N - 1>(sizes, x))>(
      size, make_vector_detail<Tp, N - 1>(sizes, x));
}

template <typename Tp, size_t N>
auto make_vector(const size_t (&sizes)[N], const Tp &x) {
  std::vector<size_t> s(N);
  for (size_t i = 0; i < N; ++i)
    s[i] = sizes[N - i - 1];
  return make_vector_detail<Tp, N>(s, x);
}
// えびちゃんの make_vector (ここまで)

signed main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());

  vector<pair<int, int>> p(m);

  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    p[i].first = c;
    p[i].second = b;
  }

  sort(p.rbegin(), p.rend());
  for (int i = 0; i < m; i++)
    cerr << p[i].first << " " << p[i].second << endl;

  int idx = 0;

  for (int i = 0; i < m; i++) {
    if (idx == n)
      break;

    int c = p[i].first;
    int b = p[i].second;
    for (int j = 0; j < b; j++) {
      if (idx == n)
        break;
      if (a[idx] < c) {
        a[idx] = c;
        idx++;

      } else {
        idx = n;
        break;
      }
    }
  }

  for (int i = 0; i < n; i++)
    cerr << a[i] << " ";
  cerr << endl;

  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += a[i];
  cout << ans << endl;

  return 0;
}