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

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

int main() {
  long n, m, k = 0;
  cin >> n >> m;
  vector<long> A(n);
  vector<pair<long, long>> BC(m);
  vector<long> change;
  For(i, n) { cin >> A.at(i); }
  For(i, m) {
    long b, c;
    cin >> b >> c;
    BC[i] = make_pair(c, b);
    k += b;
  }
  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end()); // firstが数字 secondが個数

  long t = 0;
  long num = m - 1;
  long kosuu = 0;
  while (A.at(t) < BC[num].first) {
    A.at(t) = BC[num].first;
    t++;
    kosuu++;
    if (kosuu >= BC[num].second) {
      num--;
      kosuu = 0;
    }
    if (t >= k) {
      break;
    }
  }
  long ans = 0;
  For(i, n) {
    // cout<<A.at(i)<<endl;
    ans += A.at(i);
  }
  cout << ans << endl;
  // For(i,m){
  // cout<<BC[i].first<<BC[i].second<<endl;
  //}
}


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

int main() {
  long n, m, k = 0;
  cin >> n >> m;
  vector<long> A(n);
  vector<pair<long, long>> BC(m);
  vector<long> change;
  For(i, n) { cin >> A.at(i); }
  For(i, m) {
    long b, c;
    cin >> b >> c;
    BC[i] = make_pair(c, b);
    k += b;
  }
  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end()); // firstが数字 secondが個数

  long t = 0;
  long num = m - 1;
  long kosuu = 0;
  while (A.at(t) < BC[num].first) {
    A.at(t) = BC[num].first;
    t++;
    kosuu++;
    if (kosuu >= BC[num].second) {
      num--;
      kosuu = 0;
    }
    if (t >= k || t >= n) {
      break;
    }
  }
  long ans = 0;
  For(i, n) {
    // cout<<A.at(i)<<endl;
    ans += A.at(i);
  }
  cout << ans << endl;
  // For(i,m){
  // cout<<BC[i].first<<BC[i].second<<endl;
  //}
}
Problem p03038 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  int b[m];
  int c[m];
  bool end = false;
  vector<pair<long, long>> pairs(m);
  fr(i, n) { cin >> a[i]; }
  fr(i, m) {
    cin >> b[i] >> c[i];
    pairs[i] = make_pair(c[i], b[i]);
  }
  sort(pairs.begin(), pairs.end());
  sort(a, a + n);
  int k = 0;
  ifr(i, m) {
    fr(j, pairs[i].second) {
      if (a[k] < pairs[i].first)
        a[k] = pairs[i].first;
      else {
        end = true;
        break;
      }
      k++;
    }
    if (end == true)
      break;
  }
  ll ans = 0;
  fr(i, n) { ans += a[i]; }
  cout << ans << endl;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  int b[m];
  int c[m];
  bool end = false;
  vector<pair<long, long>> pairs(m);
  fr(i, n) { cin >> a[i]; }
  fr(i, m) {
    cin >> b[i] >> c[i];
    pairs[i] = make_pair(c[i], b[i]);
  }
  sort(pairs.begin(), pairs.end());
  sort(a, a + n);
  int k = 0;
  ifr(i, m) {
    fr(j, pairs[i].second) {
      if (a[k] < pairs[i].first)
        a[k] = pairs[i].first;
      else {
        end = true;
        break;
      }
      k++;
      if (k >= n) {
        end = true;
        break;
      }
    }
    if (end == true)
      break;
  }
  ll ans = 0;
  fr(i, n) { ans += a[i]; }
  cout << ans << endl;
}
Problem p03038 (C++)

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

int n, m;
int a[100002];
int b, c;
priority_queue<pair<int, int>> pq;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    pq.push(make_pair(c, b));
  }

  int cnt = 0;
  while (!pq.empty()) {
    pair<int, int> f = pq.top();
    pq.pop();
    int val = f.first, lim = f.second;
    while (0 < lim && a[cnt] < val) {
      a[cnt] = val;
      cnt++;
      lim--;
    }
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }

  cout << ans << endl;
  return (0);
}

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

int n, m;
int a[100002];
int b, c;
priority_queue<pair<int, int>> pq;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    pq.push(make_pair(c, b));
  }

  int cnt = 0;
  while (!pq.empty()) {
    pair<int, int> f = pq.top();
    pq.pop();
    int val = f.first, lim = f.second;
    while (0 < lim && a[cnt] < val && cnt < n) {
      a[cnt] = val;
      cnt++;
      lim--;
    }
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    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(M);
  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.at(t) = 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 && j < N) {
      if (A.at(j) < T && j < N) {
        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(M);
  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.at(t) = 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)
      break;
    if (A.at(j) > T)
      break;
    while (S > 0 && j < N) {
      if (A.at(j) < T && j < N) {
        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 <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)

int main() {
  vector<pair<int, int>> so;
  vector<int> a;
  long long ans = 0;
  int n, m, count = 0;
  cin >> n >> m;
  a.resize(n);
  so.resize(m);
  REP(i, n) { cin >> a[i]; }
  REP(i, m) { cin >> so[i].second >> so[i].first; }
  sort(a.begin(), a.end(), greater<int>());
  sort(so.begin(), so.end(), greater<pair<int, int>>());
  int j = 0;
  int su = 0;
  while (su < n) {
    REP(i, so[j].second) { a.push_back(so[j].first); }
    su += so[j].second;
    j++;
  }
  sort(a.begin(), a.end(), greater<int>());
  REP(i, n) { ans += a[i]; }
  cout << ans;
  return (0);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)

int main() {
  vector<pair<int, int>> so;
  vector<int> a;
  long long ans = 0;
  int n, m, count = 0;
  cin >> n >> m;
  a.resize(n);
  so.resize(m);
  REP(i, n) { cin >> a[i]; }
  REP(i, m) { cin >> so[i].second >> so[i].first; }
  sort(a.begin(), a.end(), greater<int>());
  sort(so.begin(), so.end(), greater<pair<int, int>>());
  int j = 0;
  int su = 0;
  while (su < n) {
    REP(i, so[j].second) { a.push_back(so[j].first); }
    su += so[j].second;
    j++;
    if (j >= m)
      break;
  }
  sort(a.begin(), a.end(), greater<int>());
  REP(i, n) { ans += a[i]; }
  cout << ans;
  return (0);
}
Problem p03038 (C++)

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

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;
}

using ll = long long;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> vv(M, vector<ll>(2));
  vector<ll> v(N);
  for (ll i = 0LL; i < N; i++) {
    cin >> v.at(i);
  }
  ll cnt = 0;
  for (ll i = 0; i < M; i++) {
    cin >> vv.at(i).at(1) >> vv.at(i).at(0);
  }
  vector<ll> va;
  sort(vv.begin(), vv.end());
  reverse(vv.begin(), vv.end());
  sort(v.begin(), v.end());
  for (ll i = 0; i < M; i++) {
    for (ll j = 0; j < vv.at(i).at(1); j++) {
      va.push_back(vv.at(i).at(0));
      if (va.size() >= N)
        break;
    }
  }
  ll i = 0LL;
  while (i < N && va.at(i) > v.at(i)) {
    v.at(i) = va.at(i);
    i++;
  }
  ll ans = 0LL;
  for (ll j = 0; j < N; j++) {
    ans += v.at(j);
  }
  cout << ans << endl;
}


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

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;
}

using ll = long long;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> vv(M, vector<ll>(2));
  vector<ll> v(N);
  for (ll i = 0LL; i < N; i++) {
    cin >> v.at(i);
  }
  ll cnt = 0;
  for (ll i = 0; i < M; i++) {
    cin >> vv.at(i).at(1) >> vv.at(i).at(0);
  }
  vector<ll> va;
  sort(vv.begin(), vv.end());
  reverse(vv.begin(), vv.end());
  sort(v.begin(), v.end());
  for (ll i = 0; i < M; i++) {
    for (ll j = 0; j < vv.at(i).at(1); j++) {
      va.push_back(vv.at(i).at(0));
      if (va.size() >= N)
        break;
    }
  }
  ll i = 0LL;
  while (i < N && i < va.size() && va.at(i) > v.at(i)) {
    v.at(i) = va.at(i);
    i++;
  }
  ll ans = 0LL;
  for (ll j = 0; j < N; j++) {
    ans += v.at(j);
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  rep(i, n) cin >> a[i];
  vector<pair<int, int>> pairs(m);
  int b, c;
  rep(i, m) {
    cin >> b >> c;
    pairs[i] = make_pair(c, b);
  }

  sort(a, a + n);
  sort(pairs.begin(), pairs.end(), greater<pair<int, int>>());

  int work = 0;
  rep(i, m) {
    rep(j, pairs[i].second) {
      if (j + work >= n)
        break;
      if (a[j + work] <= pairs[i].first)
        a[j + work] = pairs[i].first;
    }
    work += pairs[i].second;
  }

  long long sum = 0;
  rep(i, n) sum += a[i];

  cout << sum;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  rep(i, n) cin >> a[i];
  vector<pair<int, int>> pairs(m);
  int b, c;
  rep(i, m) {
    cin >> b >> c;
    pairs[i] = make_pair(c, b);
  }

  sort(a, a + n);
  sort(pairs.begin(), pairs.end(), greater<pair<int, int>>());

  int work = 0;
  rep(i, m) {
    rep(j, pairs[i].second) {
      if (j + work >= n)
        break;
      if (a[j + work] <= pairs[i].first)
        a[j + work] = pairs[i].first;
    }
    work += pairs[i].second;
    if (work >= n)
      break;
  }

  long long sum = 0;
  rep(i, n) sum += a[i];

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  long N, M;
  long A;
  long B, C;
  set<long> s;
  vector<pair<long, long>> vec;
  map<long, long> m;
  long sum = 0;

  cin >> N >> M;
  for (long i = 0; i < N; i++) {
    cin >> A;
    m[A]++;
    s.insert(A);
  }

  for (long i = 0; i < M; i++) {
    cin >> B >> C;
    vec.push_back(make_pair(C, B));
  }
  // cout << 1 << endl;
  sort(vec.begin(), vec.end(), greater<pair<long, long>>());
  // cout << 2 << endl;
  // cout << vec[0].first;
  for (long i = 0; i < M; i++) {
    long count = 0;
    for (auto itr = s.begin();
         itr != s.end() && !s.empty() && count < vec[i].second; count++) {
      auto tmp = vec[i].first;
      // cout << *itr << endl;
      if (tmp < *itr) {
        if (itr == s.begin()) {
          for (auto itr = m.begin(); itr != m.end(); ++itr) {
            sum += itr->first * itr->second;
          }
          cout << sum << endl;
          return 0;
        }
        break;
      } else {
        s.insert(tmp);
        m[*itr]--;
        if (m[*itr] == 0) {
          itr = s.erase(itr);
        }
        m[tmp]++;
      }
      // cout << "count: "  << count << endl;
    }
  }
  // cout << 3 << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    sum += itr->first * itr->second;
  }
  cout << sum << endl;

  return 0;
}

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

using namespace std;

int main() {
  long N, M;
  long A;
  long B, C;
  set<long> s;
  vector<pair<long, long>> vec;
  map<long, long> m;
  long sum = 0;

  cin >> N >> M;
  for (long i = 0; i < N; i++) {
    cin >> A;
    m[A]++;
    s.insert(A);
  }

  for (long i = 0; i < M; i++) {
    cin >> B >> C;
    vec.push_back(make_pair(C, B));
  }
  // cout << 1 << endl;
  sort(vec.begin(), vec.end(), greater<pair<long, long>>());
  // cout << 2 << endl;
  // cout << vec[0].first;
  for (long i = 0; i < M; i++) {
    long count = 0;
    for (auto itr = s.begin();
         itr != s.end() && !s.empty() && count < vec[i].second; count++) {
      auto tmp = vec[i].first;
      // cout << *itr << endl;
      if (tmp < *itr) {
        if (itr == s.begin()) {
          for (auto itr = m.begin(); itr != m.end(); ++itr) {
            sum += itr->first * itr->second;
          }
          cout << sum << endl;
          return 0;
        }
        break;
      } else {
        // s.insert(tmp);
        m[*itr]--;
        if (m[*itr] == 0) {
          itr = s.erase(itr);
        }
        m[tmp]++;
      }
      // cout << "count: "  << count << endl;
    }
  }
  // cout << 3 << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    sum += itr->first * itr->second;
  }
  cout << sum << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SE second
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define mp make_pair
#define ll long long
#define LLI long long int
#define REP(i, a, b) for (long long i = a; i < b; i++)
#define PER(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
#define Arrays(a, size) sort((a), (a) + (size))
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
using namespace std;

ll A[100001];
vector<pair<ll, ll>> pairs;
ll n, m;
int main() {
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  sort(A, A + n);
  for (ll i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    pairs.pb(make_pair(c, b));
  }
  RSORT(pairs);
  ll inserted = 0, move = 0;
  for (ll i = 0; i < m; i++) {
    ll c = pairs[i].first, b = pairs[i].second;
    for (ll j = 0; j < b; j++) {
      if (A[j + move] >= c)
        break;
      A[j + move] = c;
      inserted++;
    }
    move = inserted;
  }
  ll sum = 0;
  for (ll i = 0; i < n; i++)
    sum += A[i];
  cout << sum << endl;
};

Fixed submission:
#include <bits/stdc++.h>
#define SE second
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define mp make_pair
#define ll long long
#define LLI long long int
#define REP(i, a, b) for (long long i = a; i < b; i++)
#define PER(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
#define Arrays(a, size) sort((a), (a) + (size))
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
using namespace std;

ll A[100001];
vector<pair<ll, ll>> pairs;
ll n, m;
int main() {
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  sort(A, A + n);
  for (ll i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    pairs.pb(make_pair(c, b));
  }
  RSORT(pairs);
  ll inserted = 0, move = 0;
  for (ll i = 0; i < m; i++) {
    ll c = pairs[i].first, b = pairs[i].second;
    for (ll j = 0; j < b; j++) {
      if (A[j + move] >= c || j + move > n)
        break;
      A[j + move] = c;
      inserted++;
    }
    move = inserted;
  }
  ll sum = 0;
  for (ll i = 0; i < n; i++)
    sum += A[i];
  cout << sum << endl;
};
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define i64 int64_t
#define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++)
#define ffrvs(ii, nn, mm) for (int ii = nn; ii >= mm; ii--)
#define srt(vvv) sort(vvv.begin(), vvv.end())
#define srtrvs(vvv) reverse(vvv.begin(), vvv.end())
#define Graph vector<vector<int>>
i64 inf = 1000000007;
i64 giant = 4000000000000000037;

using namespace std;

// メモリ置き場
int n, m;
vector<i64> a;
deque<vector<i64>> c;

// inputします
void input() {
  cin >> n >> m;
  a.resize(n);
  ff(i, 0, n) { cin >> a[i]; }
  ff(i, 0, m) {
    int b, d;
    cin >> b >> d;
    c.push_back({d, b});
  }
  srt(c);
  srtrvs(c);
  srt(a);
}

// ライブラリ置き場

// main
int main() {
  input();
  ff(i, 0, n) {
    if (a[i] < c[0][0]) {
      a[i] = c[0][0];
      c[0][1]--;
      if (c[0][1] == 0)
        c.pop_front();
    } else
      break;
  }
  i64 ans = 0;
  ff(i, 0, n) { ans += a[i]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define i64 int64_t
#define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++)
#define ffrvs(ii, nn, mm) for (int ii = nn; ii >= mm; ii--)
#define srt(vvv) sort(vvv.begin(), vvv.end())
#define srtrvs(vvv) reverse(vvv.begin(), vvv.end())
#define Graph vector<vector<int>>
i64 inf = 1000000007;
i64 giant = 4000000000000000037;

using namespace std;

// メモリ置き場
int n, m;
vector<i64> a;
deque<vector<i64>> c;

// inputします
void input() {
  cin >> n >> m;
  a.resize(n);
  ff(i, 0, n) { cin >> a[i]; }
  ff(i, 0, m) {
    int b, d;
    cin >> b >> d;
    c.push_back({d, b});
  }
  srt(c);
  srtrvs(c);
  srt(a);
}

// ライブラリ置き場

// main
int main() {
  input();
  ff(i, 0, n) {
    if (c.empty())
      break;
    if (a[i] < c[0][0]) {
      a[i] = c[0][0];
      c[0][1]--;
      if (c[0][1] == 0)
        c.pop_front();
    } else
      break;
  }
  i64 ans = 0;
  ff(i, 0, n) { ans += a[i]; }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
/**/
#include <random>
using namespace std;

int main() {
  long long int N, M;
  cin >> N >> M;
  vector<long long int> A(N);
  priority_queue<long long int, vector<long long int>, greater<long long int>>
      que;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    que.push(A[i]);
  }

  vector<pair<long long int, long long int>> BC(M);
  for (size_t i = 0; i < M; i++) {
    cin >> BC[i].second >> BC[i].first;
  }

  sort(BC.begin(), BC.end());

  for (size_t i = 0; i < M; i++) {
    long long int nowV = BC[i].first, nowCount = BC[i].second;

    for (size_t j = 0; j < nowCount; j++) {
      long long int now = que.top();
      que.pop();

      if (now < nowV) {
        que.push(nowV);
      } else {
        que.push(now);
        break;
      }
    }
  }

  long long int ans = 0;
  while (que.size()) {
    long long int now = que.top();
    que.pop();
    ans += now;
  }

  cout << ans << endl;
}


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

int main() {
  long long int N, M;
  cin >> N >> M;
  vector<long long int> A(N);
  priority_queue<long long int, vector<long long int>, greater<long long int>>
      que;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    que.push(A[i]);
  }

  vector<pair<long long int, long long int>> BC(M);
  for (size_t i = 0; i < M; i++) {
    cin >> BC[i].second >> BC[i].first;
  }

  sort(BC.begin(), BC.end());

  long long int countSum = 0;
  for (int i = M - 1; i >= 0; i--) {
    BC[i].second = min(N - countSum, BC[i].second);
    countSum += BC[i].second;
  }

  for (size_t i = 0; i < M; i++) {
    long long int nowV = BC[i].first, nowCount = BC[i].second;

    for (size_t j = 0; j < nowCount; j++) {
      long long int now = que.top();
      que.pop();

      if (now < nowV) {
        que.push(nowV);
      } else {
        que.push(now);
        break;
      }
    }
  }

  long long int ans = 0;
  while (que.size()) {
    long long int now = que.top();
    que.pop();
    ans += now;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  vector<pair<ll, ll>> b(n);
  rep(i, m) cin >> b[i].second >> b[i].first;
  sort(rall(b));
  ll j = 0;
  rep(i, n) {
    if (a[i] < b[j].first) {
      a[i] = b[j].first;
      b[j].second--;
    }
    if (b[j].second <= 0)
      j++;
    if (j >= m)
      break;
  }
  cout << accumulate(all(a), 0LL) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  vector<pair<ll, ll>> b(m);
  rep(i, m) cin >> b[i].second >> b[i].first;
  sort(rall(b));
  ll j = 0;
  rep(i, n) {
    if (a[i] < b[j].first) {
      a[i] = b[j].first;
      b[j].second--;
    }
    if (b[j].second <= 0)
      j++;
    if (j >= m)
      break;
  }
  cout << accumulate(all(a), 0LL) << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 5e5 + 5;
bool a[MAX];

int main() {
  LL n, m;
  cin >> n >> m;
  multiset<LL> s;
  for (int i = 0; i < n; i++) {
    LL x;
    cin >> x;
    s.insert(x);
  }
  while (m--) {
    LL b, c;
    cin >> b >> c;
    while (b--) {

      LL x = *(s.begin());
      if (x >= c)
        break;
      // cout<<x<<" ";
      s.erase(s.begin());
      x = c;
      s.insert(x);
    }
  }
  LL sum = 0;
  for (auto x : s) {
    // cout<<x<<" ";
    sum += x;
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 5e5 + 5;
bool a[MAX];

int main() {
  LL n, m;
  cin >> n >> m;
  multiset<LL> s;
  for (int i = 0; i < n; i++) {
    LL x;
    cin >> x;
    s.insert(x);
  }
  vector<pair<LL, LL>> v;
  for (int i = 0; i < m; i++) {
    LL x, y;
    cin >> x >> y;
    v.push_back(make_pair(y, x));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < m; i++) {
    LL b = v[i].second;
    LL c = v[i].first;
    while (b--) {

      LL x = *(s.begin());
      if (x >= c)
        break;
      // cout<<x<<" ";
      s.erase(s.begin());
      x = c;
      s.insert(x);
    }
  }
  LL sum = 0;
  for (auto x : s) {
    // cout<<x<<" ";
    sum += x;
  }
  cout << sum << endl;
}
Problem p03038 (C++)

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

#define int long long
#define sp ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define mod (long long)1000000007
#define f first
#define s second
#define debug1(x) cerr << x << "\n"
#define debug2(x, y) cerr << x << " " << y << "\n"
#define debug3(x, y, z) cerr << x << " " << y << " " << z << "\n"
#define nl cerr << "\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cerr << "abcd\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define mxn 500005

int32_t main() {
  sp;
  int n, m;
  cin >> n >> m;
  pq<int, vector<int>, greater<int>> q;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    q.push(x);
  }
  while (m--) {
    int c, b;
    cin >> b >> c;
    while (b-- && q.empty() == 0 && q.top() < c) {
      q.pop();
      q.push(c);
    }
  }
  int sum = 0;
  while (!q.empty()) {
    // cout<<q.top()<<" ";
    sum += q.top();
    q.pop();
  }
  cout << sum;
  return 0;
}


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

#define int long long
#define sp ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define mod (long long)1000000007
#define f first
#define s second
#define debug1(x) cerr << x << "\n"
#define debug2(x, y) cerr << x << " " << y << "\n"
#define debug3(x, y, z) cerr << x << " " << y << " " << z << "\n"
#define nl cerr << "\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cerr << "abcd\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define mxn 500005

int32_t main() {
  sp;
  int n, m;
  cin >> n >> m;
  pq<int, vector<int>, greater<int>> q;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    q.push(x);
  }
  vector<pi> v(m);
  for (int i = 0; i < m; ++i)
    cin >> v[i].s >> v[i].f;
  sort(v.rbegin(), v.rend());
  for (auto &it : v) {
    int b = it.s;
    int c = it.f;
    while (b-- && q.empty() == 0 && q.top() < c) {
      q.pop();
      q.push(c);
    }
  }
  int sum = 0;
  while (!q.empty()) {
    // cout<<q.top()<<" ";
    sum += q.top();
    q.pop();
  }
  cout << sum;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;

int main() {
  freopen("D.INP", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  priority_queue<int, vector<int>, greater<int>> q;
  priority_queue<pair<int, int>> o;
  cin >> n >> m;

  while (n--) {
    int t;
    cin >> t;
    q.push(t);
  }

  while (m--) {
    int b, c;
    cin >> b >> c;
    o.push({c, b});
  }

  while (!o.empty() && q.top() < o.top().first) {
    int b, c;
    b = o.top().second;
    c = o.top().first;
    o.pop();
    while (b-- && q.top() < c)
      q.pop(), q.push(c);
  }

  long long ans = 0;
  while (!q.empty())
    ans += 1ll * q.top(), q.pop();
  cout << ans << '\n';

  return 0;
}


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

using namespace std;

int main() {
  // freopen("D.INP","r",stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  priority_queue<int, vector<int>, greater<int>> q;
  priority_queue<pair<int, int>> o;
  cin >> n >> m;

  while (n--) {
    int t;
    cin >> t;
    q.push(t);
  }

  while (m--) {
    int b, c;
    cin >> b >> c;
    o.push({c, b});
  }

  while (!o.empty() && q.top() < o.top().first) {
    int b, c;
    b = o.top().second;
    c = o.top().first;
    o.pop();
    while (b-- && q.top() < c)
      q.pop(), q.push(c);
  }

  long long ans = 0;
  while (!q.empty())
    ans += 1ll * q.top(), q.pop();
  cout << ans << '\n';

  return 0;
}
Problem p03038 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<int, int>> t(n);
  for (int i = 0; i < m; i++)
    cin >> t[i].second >> t[i].first;
  sort(a.begin(), a.end());
  sort(t.begin(), t.end(), greater<pair<int, int>>());
  long long sum = 0;
  int k = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < t[i].second && k < n; j++)
      if (t[i].first > a[k]) {
        sum += t[i].first;
        k++;
      } else
        break;
  for (; k < n; k++)
    sum += a[k];
  cout << sum << endl;
  return 0;
}


Fixed submission:
// D - Integer Cards
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<int, int>> t(m);
  for (int i = 0; i < m; i++)
    cin >> t[i].second >> t[i].first;
  sort(a.begin(), a.end());
  sort(t.begin(), t.end(), greater<pair<int, int>>());
  long long sum = 0;
  int k = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < t[i].second && k < n; j++)
      if (t[i].first > a[k]) {
        sum += t[i].first;
        k++;
      } else
        break;
  for (; k < n; k++)
    sum += a[k];
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define P pair<ll, ll>

typedef vector<int> vec;
typedef vector<vec> mat;

bool f(P x, P y) {
  if (x.second != y.second)
    return x.second > y.second;
  else
    return x.first <= y.first;
}

int main() {
  ll n, m;
  cin >> n >> m;
  ll a[n];
  rep(i, n) cin >> a[i];
  P b[m];
  rep(i, m) cin >> b[i].first >> b[i].second;
  sort(a, a + n);
  sort(b, b + m, f);
  ll ans = 0;
  ll idx = 0;
  rep(i, n) {
    if (idx < m) {
      if (a[i] < b[idx].second) {
        ans += b[idx].second;
        b[idx].first--;
      } else {
        ans += a[i];
        idx = m;
      }
    } else {
      ans += a[i];
    }
    if (b[idx].first == 0)
      idx++;
  }
  // rep(i, m){
  //     cout << b[i].first << " " << b[i].second << endl;
  // }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define P pair<ll, ll>

typedef vector<int> vec;
typedef vector<vec> mat;

bool f(P x, P y) {
  if (x.second != y.second)
    return x.second > y.second;
  else
    return x.first < y.first;
}

int main() {
  ll n, m;
  cin >> n >> m;
  ll a[n];
  rep(i, n) cin >> a[i];
  P b[m];
  rep(i, m) cin >> b[i].first >> b[i].second;
  sort(a, a + n);
  sort(b, b + m, f);
  ll ans = 0;
  ll idx = 0;
  rep(i, n) {
    if (idx < m) {
      if (a[i] < b[idx].second) {
        ans += b[idx].second;
        b[idx].first--;
      } else {
        ans += a[i];
        idx = m;
      }
    } else {
      ans += a[i];
    }
    if (b[idx].first == 0)
      idx++;
  }
  // rep(i, m){
  //     cout << b[i].first << " " << b[i].second << endl;
  // }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define tl 1'000'000'007
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
struct P {
  ll b, c;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<ll> A(n);
  vector<P> T(m);
  REP(i, n) cin >> A[i];
  REP(i, m) cin >> T[i].b >> T[i].c;
  sort(A.begin(), A.end());
  sort(T.begin(), T.end(), [](const P &x, const P &y) { return x.c > y.c; });
  ll ii = 0;
  REP(i, T.size()) {
    REP(j, T[i].b) {
      // cout << A[ii] << " ";
      A[ii] = max(A[ii], T[i].c);

      ii++;
      if (ii >= n) {
        break;
      }
    }
  }
  ll sum = 0;
  REP(i, n) { sum += A[i]; }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define tl 1'000'000'007
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
struct P {
  ll b, c;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<ll> A(n);
  vector<P> T(m);
  REP(i, n) cin >> A[i];
  REP(i, m) cin >> T[i].b >> T[i].c;
  sort(A.begin(), A.end());
  sort(T.begin(), T.end(), [](const P &x, const P &y) { return x.c > y.c; });
  ll ii = 0;
  REP(i, T.size()) {
    REP(j, T[i].b) {
      // cout << A[ii] << " ";
      A[ii] = max(A[ii], T[i].c);

      ii++;
      if (ii >= n) {
        break;
      }
    }
    if (ii >= n) {
      break;
    }
  }
  ll sum = 0;
  REP(i, n) { sum += A[i]; }
  cout << sum << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int arr[100001];
vector<pair<int, int>> v;
int n, m;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first > b.first;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  sort(arr, arr + n);
  int num, val;
  for (int i = 0; i < m; i++) {
    cin >> num >> val;
    v.push_back(make_pair(val, num));
  }

  sort(v.begin(), v.end(), cmp);
  int j = 0; // v_ind
  for (int i = 0; i < m; i++) {
    if (v[i].first > arr[j] && v[i].second > 0) {
      v[i].second -= 1;
      arr[j] = v[i].first;
      i--;
      j++;
    }
  }

  long long int result = 0;
  for (int i = 0; i < n; i++)
    result += arr[i];

  cout << result;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int arr[100001];
vector<pair<int, int>> v;
int n, m;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first > b.first;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  sort(arr, arr + n);
  int num, val;
  for (int i = 0; i < m; i++) {
    cin >> num >> val;
    v.push_back(make_pair(val, num));
  }

  sort(v.begin(), v.end(), cmp);
  int j = 0; // v_ind
  for (int i = 0; i < m; i++) {
    if (v[i].first > arr[j] && v[i].second > 0 && j < n) {
      v[i].second -= 1;
      arr[j] = v[i].first;
      i--;
      j++;
    }
  }

  long long int result = 0;
  for (int i = 0; i < n; i++)
    result += arr[i];

  cout << result;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define FORe(i, k, n) for (int i = (k); i <= (n); i++)
#define FORr(i, k, n) for (int i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (int i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<P> v(n);
  REP(i, n) cin >> a[i];
  REP(i, m) {
    int b, c;
    cin >> b >> c;
    v[i] = P(c, b);
  }
  sort(ALL(a));
  sort(ALLr(v));
  ll ans = 0;
  int j = 0;
  REP(i, m) {
    int c = v[i].first;
    int b = v[i].second;
    if (a[j] >= c)
      break;
    while (j < n && a[j] < c && b > 0) {
      ans += c;
      j++;
      b--;
    }
    if (j == n)
      break;
  }
  while (j < n)
    ans += a[j++];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define FORe(i, k, n) for (int i = (k); i <= (n); i++)
#define FORr(i, k, n) for (int i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (int i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<P> v(m);
  REP(i, n) cin >> a[i];
  REP(i, m) {
    int b, c;
    cin >> b >> c;
    v[i] = P(c, b);
  }
  sort(ALL(a));
  sort(ALLr(v));
  ll ans = 0;
  int j = 0;
  REP(i, m) {
    int c = v[i].first;
    int b = v[i].second;
    if (a[j] >= c)
      break;
    while (j < n && a[j] < c && b > 0) {
      ans += c;
      j++;
      b--;
    }
    if (j == n)
      break;
  }
  while (j < n)
    ans += a[j++];
  cout << ans << 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 long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> B(M);
  vector<long long> C(N);
  for (int i = 0; i < M; i++)
    cin >> B[i] >> C[i];
  sort(A.begin(), A.end());
  vector<int> index(M);
  iota(index.begin(), index.end(), 0);
  sort(index.begin(), index.end(), [&](int i, int j) { return C[i] > C[j]; });

  int cont = 0;
  for (auto i : index) {
    for (int j = 0; j < B[i]; j++) {
      if (A[cont] > C[i])
        break;
      A[cont] = C[i];
      cont++;
    }
  }
  long long sum = 0;
  for (int i = 0; i < N; i++)
    sum += A[i];
  cout << sum << endl;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> B(M);
  vector<long long> C(M);
  for (int i = 0; i < M; i++)
    cin >> B[i] >> C[i];
  sort(A.begin(), A.end());
  vector<int> index(M);
  iota(index.begin(), index.end(), 0);
  sort(index.begin(), index.end(), [&](int i, int j) { return C[i] > C[j]; });

  int cont = 0;
  for (auto i : index) {
    for (int j = 0; j < B[i]; j++) {
      if (A[cont] > C[i])
        break;
      A[cont] = C[i];
      cont++;
    }
  }
  long long sum = 0;
  for (int i = 0; i < N; i++)
    sum += A[i];
  cout << sum << endl;
}
Problem p03038 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> a(N), c(M), d(N);
  vector<int> b(M);
  vector<pair<int, int>> pairs(M);
  ll sum = 0;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ans = sum;
  for (int i = 0; i < M; i++) {
    cin >> b[i] >> c[i];
    pairs[i] = make_pair(c[i], b[i]);
  }
  sort(a.begin(), a.end());
  sort(pairs.begin(), pairs.end(), greater<pair<int, int>>());
  int j = 0;
  for (int i = 0; i < N; i++) {
    if (pairs[j].second == 0)
      j++;
    d[i] = pairs[j].first;
    pairs[j].second--;
  }
  for (int i = 0; i < N; i++) {
    sum = sum + d[i] - a[i];
    if (sum > ans)
      ans = sum;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> a(N), c(M), d(N);
  vector<int> b(M);
  vector<pair<ll, int>> pairs(M);
  ll sum = 0;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ans = sum;
  for (int i = 0; i < M; i++) {
    cin >> b[i] >> c[i];
    pairs[i] = make_pair(c[i], b[i]);
  }
  sort(a.begin(), a.end());
  sort(pairs.begin(), pairs.end(), greater<pair<int, int>>());
  int j = 0;
  for (int i = 0; i < N; i++) {
    if (pairs[j].second == 0)
      j++;
    d[i] = pairs[j].first;
    pairs[j].second--;
  }
  for (int i = 0; i < N; i++) {
    sum = sum + d[i] - a[i];
    if (sum > ans)
      ans = sum;
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

typedef int64_t ll;
typedef uint64_t ull;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

ll ans;
int n, m, a[100000];
priority_queue<pair<int, int>> pq;
int main(void) {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    pq.push(make_pair(c, b));
  }
  int i = 0;
  while (pq.size() != 0) {
    auto p = pq.top();
    pq.pop();
    int j = 0;
    while (j < p.second) {
      if (a[i] < p.first) {
        a[i] = p.first;
        i++;
        j++;
        continue;
      }
      goto end;
    }
  }
end:
  rep(i, n) { ans += a[i]; }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

typedef int64_t ll;
typedef uint64_t ull;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

ll ans;
int n, m, a[100000];
priority_queue<pair<int, int>> pq;
int main(void) {
  cin >> n >> m;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    pq.push(make_pair(c, b));
  }
  int i = 0;
  while (pq.size() != 0) {
    auto p = pq.top();
    pq.pop();
    int j = 0;
    while (j < p.second) {
      if (a[i] < p.first) {
        a[i] = p.first;
        i++;
        j++;
        if (i == n)
          goto end;
        continue;
      }
      goto end;
    }
  }
end:
  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;
#define ll long long

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    cin >> vec[i];
  }
  sort(vec.begin(), vec.end());

  vector<pair<int, int>> card(M);
  for (ll i = 0; i < M; i++) {
    ll b, c;
    cin >> b >> c;
    card[i] = make_pair(c, b);
  }
  sort(card.begin(), card.end());

  ll now = M - 1;
  for (ll i = 0; i < N; i++) {
    if (card[now].second <= 0) {
      now--;
    }
    if (vec[i] < card[now].first) {
      vec[i] = card[now].first;
      card[now].second--;
    }
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ans += vec[i];
  }
  cout << ans << endl;
}


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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    cin >> vec[i];
  }
  sort(vec.begin(), vec.end());

  vector<pair<ll, ll>> card(M);
  for (ll i = 0; i < M; i++) {
    ll b, c;
    cin >> b >> c;
    card[i] = make_pair(c, b);
  }
  sort(card.begin(), card.end());

  ll now = M - 1;
  for (ll i = 0; i < N; i++) {
    if (card[now].second <= 0) {
      now--;
    }
    if (vec[i] < card[now].first) {
      vec[i] = card[now].first;
      card[now].second--;
    }
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ans += vec[i];
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<pii> v(n);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v[i] = make_pair(c, b);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int cnt = 0, last = n;
  ll ans = 0;
  rep(i, n) {
    if (v[cnt].second <= 0)
      cnt++;
    if (cnt < m && v[cnt].first > a[i]) {
      v[cnt].second--;
      ans += v[cnt].first;
    } else {
      last = i;
      break;
    }
  }
  for (int i = last; i < n; i++)
    ans += a[i];
  cout << ans << endl;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<pii> v(m);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v[i] = make_pair(c, b);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int cnt = 0, last = n;
  ll ans = 0;
  rep(i, n) {
    if (v[cnt].second <= 0)
      cnt++;
    if (cnt < m && v[cnt].first > a[i]) {
      v[cnt].second--;
      ans += v[cnt].first;
    } else {
      last = i;
      break;
    }
  }
  for (int i = last; i < n; i++)
    ans += a[i];
  cout << ans << endl;
}