Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int n, m;
ll ans = 0;
ll a[100001];
pii p[100001];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> p[i].se >> p[i].fr;
  sort(p, p + m);
  reverse(p, p + m);
  sort(a, a + n);
  int now = 0;
  rep(i, n) rep(i, m) {
    while (now < n && p[i].se && p[i].fr > a[now]) {
      a[now] = p[i].fr;
      now++;
      p[i].se--;
    }
    if (now == n || p[i].se)
      break;
  }

  rep(i, n) ans += a[i];
  cout << ans;
  re 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int n, m;
ll ans = 0;
ll a[100001];
pii p[100001];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> p[i].se >> p[i].fr;
  sort(p, p + m);
  reverse(p, p + m);
  sort(a, a + n);
  int now = 0;
  rep(i, m) {
    while (now < n && p[i].se && p[i].fr > a[now]) {
      a[now] = p[i].fr;
      now++;
      p[i].se--;
    }
    if (now == n || p[i].se)
      break;
  }

  rep(i, n) ans += a[i];
  cout << ans;
  re 0;
}