prithivMLmods/Coder-Stat huggingface dataset:
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = {0, -1, 1, 0};
const ll dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
///////////////////////////////////////

int main() {
  int N, M;
  vector<int> V;
  cin >> N >> M;
  ll A[100100];
  ll B[100100], C[100100];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    V.push_back(A[i]);
  }
  vector<pii> key;
  for (int i = 0; i < M; ++i) {
    cin >> B[i] >> C[i];
    key.push_back({C[i], B[i]});
  }
  sort(key.begin(), key.end(), greater<pii>());
  sort(V.begin(), V.end());
  int x = 0;
  for (int i = 0; i < M; ++i) {
    int count = 0;
    while (V[x] < key[i].first && count < key[i].second) {
      V[x] = key[i].first;
      count++;
      x++;
    }
  }
  ll sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += V[i];
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = {0, -1, 1, 0};
const ll dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
///////////////////////////////////////

int main() {
  int N, M;
  vector<int> V;
  cin >> N >> M;
  ll A[100100];
  ll B[100100], C[100100];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    V.push_back(A[i]);
  }
  vector<pii> key;
  for (int i = 0; i < M; ++i) {
    cin >> B[i] >> C[i];
    key.push_back({C[i], B[i]});
  }
  sort(key.begin(), key.end(), greater<pii>());
  sort(V.begin(), V.end());
  int x = 0;
  for (int i = 0; i < M; ++i) {
    int count = 0;
    while (V[x] < key[i].first && count < key[i].second) {
      V[x] = key[i].first;
      count++;
      x++;
    }
    if (x >= N) {
      break;
    }
  }
  ll sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += V[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll N, M;
  cin >> N >> M;
  vec A(N);
  vector<pll> P(N);
  REP(i, 0, N) cin >> A[i];
  REP(i, 0, M) cin >> P[i].second >> P[i].first;

  ASC(A);
  DESC(P);

  ll k = 0, l = 0;
  while (l < N && k < M) {
    bool f = true;
    REP(i, 0, P[k].second) {
      if (l + i >= N)
        break;
      if (A[l + i] < P[k].first)
        A[l + i] = P[k].first;
      else {
        f = false;
        l += i;
      }
    }
    if (f)
      l += P[k].second;
    else
      ++l;
    ++k;
  }

  ll sum = 0;
  REP(i, 0, N) sum += A[i];
  PR(sum);

  return 0;
}

/*



*/

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll N, M;
  cin >> N >> M;
  vec A(N);
  vector<pll> P(M);
  REP(i, 0, N) cin >> A[i];
  REP(i, 0, M) cin >> P[i].second >> P[i].first;

  ASC(A);
  DESC(P);

  ll k = 0, l = 0;
  while (l < N && k < M) {
    bool f = true;
    REP(i, 0, P[k].second) {
      if (l + i >= N)
        break;
      if (A[l + i] < P[k].first)
        A[l + i] = P[k].first;
      else {
        f = false;
        l += i;
      }
    }
    if (f)
      l += P[k].second;
    else
      ++l;
    ++k;
  }

  ll sum = 0;
  REP(i, 0, N) sum += A[i];
  PR(sum);

  return 0;
}

/*



*/
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<pair<int, int>> cb(m);
  vector<long long> d(n);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    cb[i].first = c;
    cb[i].second = b;
  }
  sort(cb.begin(), cb.end(), greater<pair<int, int>>());
  sort(a.begin(), a.end());
  int tmp = 0;
  for (int i = 0; cb.size() > i; ++i) {
    for (int j = 0; j < cb[i].second; ++j) {
      if (tmp < n)
        d[tmp++] = cb[i].first;
    }
  }
  long long ans = 0;
  tmp = 0;
  for (int i = 0; i < n; ++i) {
    if (i <= d.size() && a[i] < d[tmp])
      ans += d[tmp++];
    else
      ans += a[i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<pair<int, int>> cb(m);
  vector<long long> d(n);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    cb[i].first = c;
    cb[i].second = b;
  }
  sort(cb.begin(), cb.end(), greater<pair<int, int>>());
  sort(a.begin(), a.end());
  int tmp = 0;
  for (int i = 0; tmp < n && cb.size() > i; ++i) {
    for (int j = 0; j < cb[i].second; ++j) {
      if (tmp < n)
        d[tmp++] = cb[i].first;
    }
  }
  long long ans = 0;
  tmp = 0;
  for (int i = 0; i < n; ++i) {
    if (i <= d.size() && a[i] < d[tmp])
      ans += d[tmp++];
    else
      ans += a[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

  priority_queue<int, vector<int>, greater<int>> A;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push(tmp);
  }
  map<int, int, greater<int>> C;
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    C[c] += b;
  }

  for (auto c : C) {
    // cout << c.first << " " << c.second << endl;
    for (int i = 0; i < c.second; i++) {
      if (A.top() < c.first) {
        A.pop();
        A.push(c.first);
      }
    }
  }

  ll output = 0;
  for (int i = 0; i < N; i++) {
    output += (ll)A.top();
    A.pop();
    // cout << A[i] << endl;
  }
  cout << output << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

  priority_queue<int, vector<int>, greater<int>> A;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push(tmp);
  }
  map<int, int, greater<int>> C;
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    C[c] += b;
  }

  for (auto c : C) {
    // cout << c.first << " " << c.second << endl;
    for (int i = 0; i < c.second; i++) {
      if (A.top() < c.first) {
        A.pop();
        A.push(c.first);
      } else {
        break;
      }
    }
  }

  ll output = 0;
  for (int i = 0; i < N; i++) {
    output += (ll)A.top();
    A.pop();
    // cout << A[i] << endl;
  }
  cout << output << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<long int> a(n);
  vector<pair<long int, long int>> cb(m);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < m; i++) {
    long int b_tmp, c_tmp;
    cin >> b_tmp >> c_tmp;
    cb.at(i) = make_pair(c_tmp, b_tmp);
  }
  sort(a.rbegin(), a.rend());
  sort(cb.rbegin(), cb.rend());
  int j = 0;
  int count = 0;
  long int out = 0;
  for (int i = 0; i < n; i++) {
    if (count == n)
      break;
    while (a.at(i) <= cb.at(j).first & j < m) {
      if (n - count - cb.at(j).second >= 0) {
        out += cb.at(j).first * cb.at(j).second;
        count += cb.at(j).second;
        j++;
      } else {
        out += cb.at(j).first * (n - count);
        count += n - count;
        break;
      }
    }
    if (count == n)
      break;
    count++;
    out += a.at(i);
  }
  cout << out;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<long int> a(n);
  vector<pair<long int, long int>> cb(m);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < m; i++) {
    long int b_tmp, c_tmp;
    cin >> b_tmp >> c_tmp;
    cb.at(i) = make_pair(c_tmp, b_tmp);
  }
  sort(a.rbegin(), a.rend());
  sort(cb.rbegin(), cb.rend());
  int j = 0;
  int count = 0;
  long int out = 0;
  for (int i = 0; i < n; i++) {
    if (count == n)
      break;
    if (j < m) {
      while (a.at(i) <= cb.at(j).first) {
        if (n - count - cb.at(j).second >= 0) {
          out += cb.at(j).first * cb.at(j).second;
          count += cb.at(j).second;
          j++;
          if (j == m)
            break;
        } else {
          out += cb.at(j).first * (n - count);
          count += n - count;
          break;
        }
      }
    }
    if (count == n)
      break;
    count++;
    out += a.at(i);
  }
  cout << out;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  ll n, m;
  cin >> n >> m;

  vl cardnum(n);
  ll sum = 0;

  rep(i, n) {
    cin >> cardnum[i];
    sum += cardnum[i];
  }

  sort(all(cardnum));

  vector<pair<ll, ll>> change(m);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    change[i] = make_pair(b, a);
  }

  sort(all(change));
  reverse(all(change));

  /*for(auto p : change){
    cout << p.first << p.second;
  }
  for(ll x : cardnum){
    cout << x;
  }*/

  ll nowcard = 0;
  for (auto p : change) {
    for (ll i = 0; i < p.second; i++) {
      if (cardnum[nowcard] < p.first and nowcard != n) {
        sum += p.first - cardnum[nowcard];
        nowcard++;
      } else {
        cout << sum << endl;
        return 0;
      }
    }
  }

  cout << sum << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  ll n, m;
  cin >> n >> m;

  vl cardnum(n);
  ll sum = 0;

  rep(i, n) {
    cin >> cardnum[i];
    sum += cardnum[i];
  }

  sort(all(cardnum));

  vector<pair<ll, ll>> change(m);

  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    change[i] = make_pair(b, a);
  }

  sort(all(change));
  reverse(all(change));

  /*for(auto p : change){
    cout << p.first << p.second;
  }
  for(ll x : cardnum){
    cout << x;
  }*/

  ll nowcard = 0;
  for (auto p : change) {
    for (ll i = 0; i < p.second; i++) {
      if (nowcard != n and cardnum[nowcard] < p.first) {
        sum += p.first - cardnum[nowcard];
        nowcard++;
      } else {
        cout << sum << endl;
        return 0;
      }
    }
  }

  cout << sum << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  ll i, j, n, m, now, ans = 0, t, a;
  scanf("%lld%lld", &n, &m);
  priority_queue<ll, vector<ll>, greater<ll>> p;
  for (i = 0; i < n; ++i)
    scanf("%lld", &a), p.push(a);
  pair<ll, ll> b[m];
  for (i = 0; i < m; ++i)
    scanf("%lld%lld", &b[i].second, &b[i].first);
  sort(b, b + m, greater<pair<ll, ll>>());
  for (i = 0; i < m; ++i) {
    for (j = 0; j < b[i].second; ++j) {
      if (p.top() > b[i].first)
        break;
      p.pop(), ans += b[i].first;
    }
  }
  while (p.size())
    ans += p.top(), p.pop();
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  ll i, j, n, m, now, ans = 0, t, a;
  scanf("%lld%lld", &n, &m);
  priority_queue<ll, vector<ll>, greater<ll>> p;
  for (i = 0; i < n; ++i)
    scanf("%lld", &a), p.push(a);
  pair<ll, ll> b[m];
  for (i = 0; i < m; ++i)
    scanf("%lld%lld", &b[i].second, &b[i].first);
  sort(b, b + m, greater<pair<ll, ll>>());
  for (i = 0; i < m; ++i) {
    for (j = 0; j < b[i].second && p.size(); ++j) {
      if (p.top() > b[i].first)
        break;
      p.pop(), ans += b[i].first;
    }
  }
  while (p.size())
    ans += p.top(), p.pop();
  printf("%lld", ans);
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; 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());

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m;
  cin >> n >> m;
  vll a(n);
  REP(i, n) cin >> a[i];
  VSORT(a);
  vector<P> p;
  ll b, c;
  REP(i, m) {
    cin >> b >> c;
    p.push_back(P{c, b});
  }
  sort(p.rbegin(), p.rend());
  vll d(2 * n + 10);
  ll id = 0;
  REP(i, m) {
    c = p[i].first, b = p[i].second;
    REP(j, b) d[id + j] = c;
    id += b;
  }
  ll res = 0;
  REP(i, n) res += max(a[i], d[i]);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; 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());

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m;
  cin >> n >> m;
  vll a(n);
  REP(i, n) cin >> a[i];
  VSORT(a);
  vector<P> p;
  ll b, c;
  REP(i, m) {
    cin >> b >> c;
    p.push_back(P{c, b});
  }
  sort(p.rbegin(), p.rend());
  vll d(2 * n + 10);
  ll id = 0;
  REP(i, m) {
    c = p[i].first, b = p[i].second;
    REP(j, b) d[id + j] = c;
    id += b;
    if (id >= n)
      break;
  }
  ll res = 0;
  REP(i, n) res += max(a[i], d[i]);
  cout << res << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define FOR(NAME, START, NUM) for (ll NAME = START; NAME < (NUM); NAME++)
#define REP(NAME, NUM) for (ll NAME = 0; NAME < (NUM); NAME++)
#define BREP(NAME, NUM) for (ll NAME = (NUM)-1; NAME >= 0; NAME--)
#define ALL(NAME) (NAME).begin(), (NAME).end()
const ll INF = 1000000000000000;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<pair<ll, ll>> cb(n);
  REP(i, m) {
    cin >> cb.at(i).second;
    cin >> cb.at(i).first;
  }
  sort(ALL(a));
  sort(ALL(cb));
  reverse(ALL(cb));

  ll bsum = 0, x = 0;
  REP(i, n) {
    if (a.at(i) < cb.at(x).first)
      a.at(i) = cb.at(x).first;
    else {
      bsum = i + 1;
      x++;
      continue;
    }
    if (cb.at(x).second - 1 + bsum == i) {
      bsum += cb.at(x).second;
      x++;
    }
  }
  ll sum = 0;
  REP(i, n) { sum += a[i]; }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define FOR(NAME, START, NUM) for (ll NAME = START; NAME < (NUM); NAME++)
#define REP(NAME, NUM) for (ll NAME = 0; NAME < (NUM); NAME++)
#define BREP(NAME, NUM) for (ll NAME = (NUM)-1; NAME >= 0; NAME--)
#define ALL(NAME) (NAME).begin(), (NAME).end()
const ll INF = 1000000000000000;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<pair<ll, ll>> cb(max(m, n));
  REP(i, m) {
    cin >> cb.at(i).second;
    cin >> cb.at(i).first;
  }
  sort(ALL(a));
  sort(ALL(cb));
  reverse(ALL(cb));

  ll bsum = 0, x = 0;
  REP(i, n) {
    if (a.at(i) < cb.at(x).first)
      a.at(i) = cb.at(x).first;
    else {
      bsum = i + 1;
      x++;
      continue;
    }
    if (cb.at(x).second - 1 + bsum == i) {
      bsum += cb.at(x).second;
      x++;
    }
  }
  ll sum = 0;
  REP(i, n) { sum += a[i]; }
  cout << sum << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> p(n);
  rep(i, n) cin >> a[i];
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    p[i] = make_pair(c, b);
  }
  sort(a.begin(), a.end());
  sort(p.begin(), p.end(), greater<pair<int, int>>());

  int j = 0;
  rep(i, n) {
    if (a[i] < p[j].first) {
      a[i] = p[j].first;
      p[j].second--;
      if (p[j].second == 0) {
        if (j < m - 1)
          j++;
        else
          break;
      }
    } else {
      break;
    }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> p(m);
  rep(i, n) cin >> a[i];
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    p[i] = make_pair(c, b);
  }
  sort(a.begin(), a.end());
  sort(p.begin(), p.end(), greater<pair<int, int>>());

  int j = 0;
  rep(i, n) {
    if (a[i] < p[j].first) {
      a[i] = p[j].first;
      p[j].second--;
      if (p[j].second == 0) {
        if (j < m - 1)
          j++;
        else
          break;
      }
    } else {
      break;
    }
  }

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

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N), B(M), C(M);
  vector<P> Q;
  for (int s = 0; s < N; s++) {
    cin >> A.at(s);
  }
  for (int t = 0; t < M; t++) {
    cin >> B.at(t) >> C.at(t);
    Q.push_back(P(C.at(t), B.at(t)));
  }
  sort(Q.begin(), Q.end());
  reverse(Q.begin(), Q.end());
  sort(A.begin(), A.end());
  ll sum = 0, S = 0, T = 0;
  int j = 0;
  for (int i = 0; i < M; i++) {
    S = Q.at(i).second;
    T = Q.at(i).first;
    if (A.at(j) > T || j >= N)
      break;
    while (S > 0) {
      if (j < N && A.at(j) < T) {
        A.at(j) = T;
        S--;
      } else
        break;
      j++;
    }
  }
  for (int k = 0; k < N; k++) {
    sum += A.at(k);
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N), B(M), C(M);
  vector<P> Q;
  for (int s = 0; s < N; s++) {
    cin >> A.at(s);
  }
  for (int t = 0; t < M; t++) {
    cin >> B.at(t) >> C.at(t);
    Q.push_back(P(C.at(t), B.at(t)));
  }
  sort(Q.begin(), Q.end());
  reverse(Q.begin(), Q.end());
  sort(A.begin(), A.end());
  ll sum = 0, S = 0, T = 0;
  int j = 0;
  for (int i = 0; i < M; i++) {
    S = Q.at(i).second;
    T = Q.at(i).first;
    if (j >= N || A.at(j) > T)
      break;
    while (S > 0) {
      if (j < N && A.at(j) < T) {
        A.at(j) = T;
        S--;
      } else
        break;
      j++;
    }
  }
  for (int k = 0; k < N; k++) {
    sum += A.at(k);
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1000000000 + 7;

bool debug = 0;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<int, int>> bc(m);
  for (int i = 0; i < m; i++)
    cin >> bc[i].second >> bc[i].first;
  sort(a.begin(), a.end());
  sort(bc.begin(), bc.end(), greater<pair<int, int>>());
  int idx = 0;
  for (int i = 0; i < m; i++) {
    for (int j = idx; j < idx + bc[i].second; j++) {
      if (a[j] < bc[i].first)
        a[j] = bc[i].first;
    }
    idx += bc[i].second;
    if (idx >= n)
      break;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  cout << sum << endl;
}

int main() {
  if (debug) {
    while (true) {
      cout << "********" << endl;
      solve();
    }
  } else {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1000000000 + 7;

bool debug = 0;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<int, int>> bc(m);
  for (int i = 0; i < m; i++)
    cin >> bc[i].second >> bc[i].first;
  sort(a.begin(), a.end());
  sort(bc.begin(), bc.end(), greater<pair<int, int>>());
  int idx = 0;
  for (int i = 0; i < m; i++) {
    for (int j = idx; j < idx + bc[i].second; j++) {
      if (j >= n)
        break;
      if (a[j] < bc[i].first)
        a[j] = bc[i].first;
    }
    idx += bc[i].second;
    if (idx >= n)
      break;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  cout << sum << endl;
}

int main() {
  if (debug) {
    while (true) {
      cout << "********" << endl;
      solve();
    }
  } else {
    solve();
  }
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
  ll count = 0;
  ll N, M;
  cin >> N >> M;

  vector<ll> A(N);
  vector<pair<ll, ll>> vec(M, pair<ll, ll>());

  for (ll i = 0; i < N; i++)
    cin >> A.at(i);

  for (ll i = 0; i < M; i++)
    cin >> vec.at(i).second >> vec.at(i).first;

  sort(A.begin(), A.end());
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());

  ll ite = 0;

  for (ll i = 0; i < M; i++) {
    for (ll j = ite; vec.at(i).second > 0; j++) {
      if (A.at(j) < vec.at(i).first) {
        A.at(j) = vec.at(i).first;
        vec.at(i).second--;
        ite++;
      } else {
        for (ll k = 0; k < N; k++)
          count += A.at(k);
        cout << count << endl;
        return 0;
      }
    }
  }

  for (ll i = 0; i < N; i++)
    count += A.at(i);

  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
  ll count = 0;
  ll N, M;
  cin >> N >> M;

  vector<ll> A(N);
  vector<pair<ll, ll>> vec(M, pair<ll, ll>());

  for (ll i = 0; i < N; i++)
    cin >> A.at(i);

  for (ll i = 0; i < M; i++)
    cin >> vec.at(i).second >> vec.at(i).first;

  sort(A.begin(), A.end());
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());

  ll ite = 0;

  for (ll i = 0; i < M; i++) {
    for (ll j = ite; vec.at(i).second > 0 && j < N; j++) {
      if (A.at(j) < vec.at(i).first) {
        A.at(j) = vec.at(i).first;
        vec.at(i).second--;
        ite++;
      } else {
        for (ll k = 0; k < N; k++)
          count += A.at(k);
        cout << count << endl;
        return 0;
      }
    }
  }

  for (ll i = 0; i < N; i++)
    count += A.at(i);

  cout << count << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;
const int INF =
    1 << 29; // 十分大きい値にする, INT_MAX にしないのはオーバーフロー対策

void solve() {
  const int MAX_N = 10000, MAX_M = 10000; // 最後に数字直す
  int N, M;
  priority_queue<LL, vector<LL>, greater<int>> A;
  PLL BC[MAX_M + 1];
  cin >> N >> M;
  FOR(i, 0, N) {
    LL tmp;
    cin >> tmp;
    A.push(tmp);
  }
  FOR(i, 0, M) { cin >> BC[i].first >> BC[i].second; }

  sort(BC, BC + M, [](PLL &x, PLL &y) { return x.second > y.second; });

  FOR(i, 0, M) {
    int j = 0;
    for (j = 0; j < BC[i].first; j++) {
      if (A.top() < BC[i].second) {
        A.pop();
        A.push(BC[i].second);
      } else {
        break;
      }
    }
    if (j == 0)
      break;
  }

  LL ans = 0;
  while (!A.empty()) {
    ans += A.top();
    A.pop();
  }

  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;
const int INF =
    1 << 29; // 十分大きい値にする, INT_MAX にしないのはオーバーフロー対策

void solve() {
  const int MAX_N = 100000, MAX_M = 100000; // 最後に数字直す
  int N, M;
  priority_queue<LL, vector<LL>, greater<int>> A;
  PLL BC[MAX_M + 1];
  cin >> N >> M;
  FOR(i, 0, N) {
    LL tmp;
    cin >> tmp;
    A.push(tmp);
  }
  FOR(i, 0, M) { cin >> BC[i].first >> BC[i].second; }

  sort(BC, BC + M, [](PLL &x, PLL &y) { return x.second > y.second; });

  FOR(i, 0, M) {
    int j = 0;
    for (j = 0; j < BC[i].first; j++) {
      if (A.top() < BC[i].second) {
        A.pop();
        A.push(BC[i].second);
      } else {
        break;
      }
    }
    if (j == 0)
      break;
  }

  LL ans = 0;
  while (!A.empty()) {
    ans += A.top();
    A.pop();
  }

  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m;
ll a[100010];
vector<pli> v;
vector<ll> cand;

int main() {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    v.push_back(make_pair(c, b));
  }
  sort(v.begin(), v.end());

  int cnt = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = 0; j < v[i].second; j++) {
      cand.push_back(v[i].first);
      cnt++;
      if (cnt >= n)
        break;
    }
  }

  ll ans = 0;
  rep(i, n) { ans += max(a[i], cand[i]); }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m;
ll a[100010];
vector<pli> v;
vector<ll> cand;

int main() {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    v.push_back(make_pair(c, b));
  }
  sort(v.begin(), v.end());

  int cnt = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = 0; j < v[i].second; j++) {
      cand.push_back(v[i].first);
      cnt++;
      if (cnt >= n)
        break;
    }
  }
  rep(i, n - cnt) cand.push_back(0);

  ll ans = 0;
  rep(i, n) { ans += max(a[i], cand[i]); }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  for (auto P : mp) {
    pq.push(P);
  }
  for (int i = 0; i < M; i++) {
    int B, C;
    cin >> B >> C;
    int total = 0;
    while (1) {
      int num = pq.top().first;
      int cnt = pq.top().second;
      if (num >= C) {
        break;
      }
      pq.pop();
      if (total + cnt <= B) {
        total += cnt;
      } else {
        pq.push(make_pair(num, cnt - (B - total)));
        total = B;
        break;
      }
    }
    pq.push(make_pair(C, total));
  }
  long long ans = 0;
  while (!pq.empty()) {
    ans += (long long)pq.top().first * pq.top().second;
    pq.pop();
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  for (auto P : mp) {
    pq.push(P);
  }
  for (int i = 0; i < M; i++) {
    int B, C;
    cin >> B >> C;
    int total = 0;
    while (1) {
      if (pq.empty()) {
        break;
      }
      int num = pq.top().first;
      int cnt = pq.top().second;
      if (num >= C) {
        break;
      }
      pq.pop();
      if (total + cnt <= B) {
        total += cnt;
      } else {
        pq.push(make_pair(num, cnt - (B - total)));
        total = B;
        break;
      }
    }
    pq.push(make_pair(C, total));
  }
  long long ans = 0;
  while (!pq.empty()) {
    ans += (long long)pq.top().first * pq.top().second;
    pq.pop();
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  priority_queue<int, vector<int>, greater<int>> que;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    que.push(a);
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < N; i++) {
    int B, C;
    cin >> B >> C;
    v.push_back({C, B});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < M; i++) {
    int B = v[i].second;
    int C = v[i].first;
    while (B--) {
      if (C > que.top()) {
        que.pop();
        que.push(C);
      } else {
        break;
      }
    }
  }
  ll ans = 0;
  while (!que.empty())
    ans += que.top(), que.pop();
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  priority_queue<int, vector<int>, greater<int>> que;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    que.push(a);
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < M; i++) {
    int B, C;
    cin >> B >> C;
    v.push_back({C, B});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < M; i++) {
    int B = v[i].second;
    int C = v[i].first;
    while (B--) {
      if (C > que.top()) {
        que.pop();
        que.push(C);
      } else {
        break;
      }
    }
  }
  ll ans = 0;
  while (!que.empty())
    ans += que.top(), que.pop();
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <cstdlib>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;

  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  vector<pair<ll, ll>> sousa;
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    sousa.push_back(make_pair(c, b));
  }

  sort(sousa.rbegin(), sousa.rend());
  sort(a.begin(), a.end());

  int count = 0;
  int i = 0;
  while (i < n) {
    if (count >= m)
      break;

    ll b, c;
    b = sousa.at(count).second;
    c = sousa.at(count).first;
    int change = 0;

    while (a.at(i) < c && change < b) {
      a.at(i) = c;
      i++;
      change++;
    }

    if (!change)
      break;
    count++;
  }

  ll ans = 0;
  rep(i, n) ans += a.at(i);
  cout << ans;
  // rep(i,n) cout << a.at(i) << " ";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <cstdlib>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;

  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  vector<pair<ll, ll>> sousa;
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    sousa.push_back(make_pair(c, b));
  }

  sort(sousa.rbegin(), sousa.rend());
  sort(a.begin(), a.end());

  int count = 0;
  int i = 0;
  while (i < n) {
    if (count >= m)
      break;

    ll b, c;
    b = sousa.at(count).second;
    c = sousa.at(count).first;
    int change = 0;

    while (i < n && a.at(i) < c && change < b) {
      a.at(i) = c;
      i++;
      change++;
    }

    if (!change)
      break;
    count++;
  }

  ll ans = 0;
  rep(i, n) ans += a.at(i);
  cout << ans;
  // rep(i,n) cout << a.at(i) << " ";
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <regex>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

bool compare(const pair<long long, long long> &x,
             const pair<long long, long long> &y) {
  return x.second > y.second;
}

int main() {
  int n, m;
  vector<long long> a;
  vector<pair<long long, long long>> bc;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long a_;
    cin >> a_;
    a.push_back(a_);
  }
  sort(a.begin(), a.end());

  for (int i = 0; i < m; i++) {
    long long b_, c_;
    cin >> b_ >> c_;
    bc.push_back(make_pair(b_, c_));
  }
  sort(bc.begin(), bc.end(), compare);

  int ai = 0;
  int bci = 0;
  long long ans = 0;
  while (1) {
    if (bci == m || a[ai] >= bc[bci].second)
      break;
    if (a[ai] < bc[bci].second && bc[bci].first > 0) {
      ans += bc[bci].second;
      ai++;
      bc[bci].first--;
    } else if (bc[bci].first == 0) {
      bci++;
    }
  }
  for (; ai < n; ai++) {
    ans += a[ai];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <regex>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

bool compare(const pair<long long, long long> &x,
             const pair<long long, long long> &y) {
  return x.second > y.second;
}

int main() {
  int n, m;
  vector<long long> a;
  vector<pair<long long, long long>> bc;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long a_;
    cin >> a_;
    a.push_back(a_);
  }
  sort(a.begin(), a.end());

  for (int i = 0; i < m; i++) {
    long long b_, c_;
    cin >> b_ >> c_;
    bc.push_back(make_pair(b_, c_));
  }
  sort(bc.begin(), bc.end(), compare);

  int ai = 0;
  int bci = 0;
  long long ans = 0;
  while (1) {
    if (bci == m || a[ai] >= bc[bci].second || ai == n)
      break;
    if (a[ai] < bc[bci].second && bc[bci].first > 0) {
      ans += bc[bci].second;
      ai++;
      bc[bci].first--;
    } else if (bc[bci].first == 0) {
      bci++;
    }
  }
  for (; ai < n; ai++) {
    ans += a[ai];
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef long long int lli;
using namespace std;
typedef pair<int, int> ii;
#define int long long int

inline void swap(int &a, int &b) {
  int tmp = a;
  a = b;
  b = tmp;
}

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int _a;
    cin >> _a;
    A[i] = _a;
  }
  sort(A.begin(), A.end());
  vector<ii> BC;
  for (int i = 0; i < M; i++) {
    int _b, _c;
    cin >> _b >> _c;
    BC.push_back(ii(_b, _c));
  }
  sort(BC.begin(), BC.end(),
       [](const ii &x, const ii &y) { return x.second > y.second; });

  vector<int> C;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < BC[i].first && C.size() < N; j++) {
      C.push_back(BC[i].second);
    }
  }

  int total = 0;
  for (int i = 0; i < N; i++) {
    if (C.size() < N && A[i] < C[i])
      total += C[i];
    else
      total += A[i];
  }
  cout << total << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef long long int lli;
using namespace std;
typedef pair<int, int> ii;
#define int long long int

inline void swap(int &a, int &b) {
  int tmp = a;
  a = b;
  b = tmp;
}

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int _a;
    cin >> _a;
    A[i] = _a;
  }
  sort(A.begin(), A.end());
  vector<ii> BC;
  for (int i = 0; i < M; i++) {
    int _b, _c;
    cin >> _b >> _c;
    BC.push_back(ii(_b, _c));
  }
  sort(BC.begin(), BC.end(),
       [](const ii &x, const ii &y) { return x.second > y.second; });

  vector<int> C;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < BC[i].first && C.size() < N; j++) {
      C.push_back(BC[i].second);
    }
  }

  int total = 0;
  for (int i = 0; i < N; i++) {
    if (i < C.size() && A[i] < C[i])
      A[i] = C[i];
    total += A[i];
  }
  cout << total << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

ll mod = 1000000007ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll inf = 1ll << 60;

int n, m;
vector<ll> A(101010);
vector<pair<ll, ll>> BC(101010);

int gcc(int n, int m) {
  int a = max({n, m});
  int b = min({n, m});
  if (b == 0)
    return a;
  int c;
  while (a % b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return b;
}

int main() {
  scanf("%d%d", &n, &m);
  rp(i, 0, n) { scanf("%lld", &A.at(i)); }
  rp(i, 0, m) { scanf("%lld%lld", &BC.at(i).second, &BC.at(i).first); }
  sort(A.begin(), A.begin() + n);
  sort(BC.rbegin(), BC.rend());

  int pointer = 0;
  rp(i, 0, m) {
    rp(j, 0, BC.at(i).second) {
      if (A.at(pointer) < BC.at(i).first)
        A.at(pointer) = BC.at(i).first;
      pointer++;
    }
    if (pointer == n) {
      ll ans = 0;
      rp(i, 0, n) ans += A.at(i);
      printf("%lld\n", ans);
      return 0;
    }
  }
  ll ans = 0;
  rp(i, 0, n) ans += A.at(i);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

ll mod = 1000000007ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll inf = 1ll << 60;

int n, m;
vector<ll> A(101010);
vector<pair<ll, ll>> BC(101010);

int gcc(int n, int m) {
  int a = max({n, m});
  int b = min({n, m});
  if (b == 0)
    return a;
  int c;
  while (a % b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return b;
}

int main() {
  scanf("%d%d", &n, &m);
  rp(i, 0, n) { scanf("%lld", &A.at(i)); }
  rp(i, 0, m) { scanf("%lld%lld", &BC.at(i).second, &BC.at(i).first); }
  sort(A.begin(), A.begin() + n);
  sort(BC.rbegin(), BC.rend());

  int pointer = 0;
  rp(i, 0, m) {
    rp(j, 0, BC.at(i).second) {
      if (A.at(pointer) < BC.at(i).first)
        A.at(pointer) = BC.at(i).first;
      pointer++;
      if (pointer == n) {
        ll ans = 0;
        rp(i, 0, n) ans += A.at(i);
        printf("%lld\n", ans);
        return 0;
      }
    }
  }
  ll ans = 0;
  rp(i, 0, n) ans += A.at(i);
  printf("%lld\n", ans);
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
const int maxn = 1e5 + 5;
int n, m, a[maxn];
struct Op {
  int b, c;
  bool operator<(Op b) const { return c > b.c; }
} p[maxn];
long long ans;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), ans += a[i];
  for (int i = 1; i <= m; ++i)
    scanf("%d %d", &p[i].b, &p[i].c);
  std ::sort(a + 1, a + n + 1);
  std ::sort(p + 1, p + m + 1);
  for (int i = 1, j = 0; i <= m; ++i) {
    while (p[i].b && a[j + 1] < p[i].c)
      ++j, --p[i].b, ans += p[i].c - a[j];
    if (p[i].b)
      break;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
const int maxn = 1e5 + 5;
int n, m, a[maxn];
struct Op {
  int b, c;
  bool operator<(Op b) const { return c > b.c; }
} p[maxn];
long long ans;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), ans += a[i];
  for (int i = 1; i <= m; ++i)
    scanf("%d %d", &p[i].b, &p[i].c);
  std ::sort(a + 1, a + n + 1);
  std ::sort(p + 1, p + m + 1);
  for (int i = 1, j = 0; i <= m; ++i) {
    while (p[i].b && a[j + 1] < p[i].c && j < n)
      ++j, --p[i].b, ans += p[i].c - a[j];
    if (p[i].b)
      break;
  }
  printf("%lld", ans);
  return 0;
}