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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
using namespace std;
typedef long long ll;
// 定義場所//
ll i, j, k, l, m, n;
ll N, M, K;
ll MOD = 1000000007;
ll ans = 0;
////////////

bool pairCompare(const pair<ll, ll> firstElof, pair<ll, ll> secondElof) {
  return firstElof.second > secondElof.second;
}

int main() {
  cin >> N >> M;
  vector<ll> A;
  vector<pair<ll, ll>> BC(N);
  ll ans = 0;
  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());
  for (i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    BC[i] = make_pair(b, c);
  }
  sort(BC.begin(), BC.end(), pairCompare);
  // reverse(BC.begin(), BC.end());
  int depth = 0;
  int count = 0;
  for (i = 0; i < N; i++) {
    if (A[i] < BC[depth].second) {
      A[i] = BC[depth].second;
      count++;
    } else {
      break;
    }
    if (count >= BC[depth].first) {
      depth++;
      count = 0;
      if (depth > M - 1) {
        break;
      }
    }
  }
  for (i = 0; i < N; i++) {
    ans += A[i];
  }

  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
using namespace std;
typedef long long ll;
// 定義場所//
ll i, j, k, l, m, n;
ll N, M, K;
ll MOD = 1000000007;
ll ans = 0;
////////////

bool pairCompare(const pair<ll, ll> firstElof, pair<ll, ll> secondElof) {
  return firstElof.second > secondElof.second;
}

int main() {
  cin >> N >> M;
  vector<ll> A;
  vector<pair<ll, ll>> BC(100000);
  ll ans = 0;
  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());
  for (i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    BC[i] = make_pair(b, c);
  }
  sort(BC.begin(), BC.end(), pairCompare);
  // reverse(BC.begin(), BC.end());
  int depth = 0;
  int count = 0;
  for (i = 0; i < N; i++) {
    if (A[i] < BC[depth].second) {
      A[i] = BC[depth].second;
      count++;
    } else {
      break;
    }
    if (count >= BC[depth].first) {
      depth++;
      count = 0;
      if (depth > M - 1) {
        break;
      }
    }
  }
  for (i = 0; i < N; i++) {
    ans += A[i];
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define maxn 1000000007
#define PI 3.14159265358979323846
#define MAX 1000007
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout<<fixed<<setprecision(20);
  ll n, m;
  cin >> n >> m;
  std::vector<ll> a;
  vector<ll>::iterator it;
  multiset<ll> s;
  multiset<ll>::iterator it1, it2;
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    a.pb(x);
    s.insert(x);
    sum += x;
  }
  // cout<<sum<<endl;
  pair<ll, ll> b[m];
  for (int i = 0; i < m; ++i) {
    cin >> b[i].ss >> b[i].ff;
  }
  sort(b, b + m);
  for (int i = 0; i < m; ++i) {
    ll x = 0;
    while (*s.begin() < b[i].ff) {
      s.erase(s.begin());
      s.insert(b[i].ff);
      x++;
      if (x == b[i].ss) {
        break;
      }
    }
  }
  sum = 0;
  for (it1 = s.begin(); it1 != s.end(); it1++) {
    sum += *it1;
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define maxn 1000000007
#define PI 3.14159265358979323846
#define MAX 1000007
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout<<fixed<<setprecision(20);
  ll n, m;
  cin >> n >> m;
  std::vector<ll> a;
  vector<ll>::iterator it;
  multiset<ll> s;
  multiset<ll>::iterator it1, it2;
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    a.pb(x);
    s.insert(x);
    sum += x;
  }
  // cout<<sum<<endl;
  pair<ll, ll> b[m];
  for (int i = 0; i < m; ++i) {
    cin >> b[i].ss >> b[i].ff;
  }
  sort(b, b + m);
  for (int i = m - 1; i >= 0; i--) {
    ll x = 0;
    while (*s.begin() < b[i].ff) {
      s.erase(s.begin());
      s.insert(b[i].ff);
      x++;
      if (x == b[i].ss) {
        break;
      }
    }
  }
  sum = 0;
  for (it1 = s.begin(); it1 != s.end(); it1++) {
    sum += *it1;
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

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

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<P> cb(m);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    cb.at(i) = make_pair(c, b);
  }
  // ソート
  sort(a.begin(), a.end());
  sort(cb.rbegin(), cb.rend());
  int ai = 0;
  for (auto ccbb : cb) {
    rep(i, ccbb.second) {
      if (a.at(ai) < ccbb.first) {
        a.at(ai) = ccbb.first;
        ai++;
      }
      if (ai >= n) {
        break;
      }
    }
    if (ai >= n) {
      break;
    }
  }

  ll res = 0;
  rep(i, n) { res += a.at(i); }
  cout << res << endl;
  return 0;
}


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

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<P> cb(m);
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    cb.at(i) = make_pair(c, b);
  }
  // ソート
  sort(a.begin(), a.end());
  sort(cb.rbegin(), cb.rend());
  int ai = 0;
  for (auto ccbb : cb) {
    rep(i, ccbb.second) {
      if (a.at(ai) < ccbb.first) {
        a.at(ai) = ccbb.first;
        ai++;
      } else {
        break;
      }
      if (ai >= n) {
        break;
      }
    }
    if (ai >= n) {
      break;
    }
  }

  ll res = 0;
  rep(i, n) { res += a.at(i); }
  cout << res << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for (ll i = 0; i < (n); i++)
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define forn3(i, x, n) for (ll i = (x); i < (n); i++)

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<int> id(n);
  vector<pair<ll, ll>> cb;
  ll sum = 0;
  fori(n) {
    cin >> a[i];
    sum += a[i];
    id[i] = i;
  }

  fori(m) {
    ll b, c;
    cin >> b >> c;
    cb.push_back({c, b});
  }

  sort(cb.begin(), cb.end());
  sort(id.begin(), id.end(), [&](int i1, int i2) { return a[i1] < a[i2]; });

  int nextCb = m - 1;
  int nextA = 0;
  while (nextCb >= 0) {
    int d = cb[nextCb].first - a[id[nextA]];
    if (d > 0) {
      sum += d;
      nextA++;
      cb[nextCb].second--;
      if (cb[nextCb].second == 0)
        nextCb--;
    } else
      break;
  }

  cout << sum << endl;
}


Fixed submission:
/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for (ll i = 0; i < (n); i++)
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define forn3(i, x, n) for (ll i = (x); i < (n); i++)

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<int> id(n);
  vector<pair<ll, ll>> cb;
  ll sum = 0;
  fori(n) {
    cin >> a[i];
    sum += a[i];
    id[i] = i;
  }

  fori(m) {
    ll b, c;
    cin >> b >> c;
    cb.push_back({c, b});
  }

  sort(cb.begin(), cb.end());
  sort(id.begin(), id.end(), [&](int i1, int i2) { return a[i1] < a[i2]; });

  int nextCb = m - 1;
  int nextA = 0;
  while (nextCb >= 0 && nextA < n) {
    int d = cb[nextCb].first - a[id[nextA]];
    if (d > 0) {
      sum += d;
      nextA++;
      cb[nextCb].second--;
      if (cb[nextCb].second == 0)
        nextCb--;
    } else
      break;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
typedef long long ll;

int N, M, H, W, K, A, B;
string S;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  cin >> N >> M;
  vector<ll> a(N, 0);
  vector<pair<ll, ll>> cb(M, pair<ll, ll>(0, 0));
  rep(i, N) cin >> a.at(i);
  rep(i, M) cin >> cb.at(i).se >> cb.at(i).fi;
  sort(a.begin(), a.end());
  sort(cb.begin(), cb.end());
  reverse(cb.begin(), cb.end());
  int card = 0;
  bool fin = false;
  rep(i, M) {
    rep(j, cb.at(i).se) {
      if (a.at(card) > cb.at(i).fi || card == N) {
        fin = true;
        break;
      }
      a.at(card) = cb.at(i).fi;
      card++;
    }
    if (fin)
      break;
  }
  ll ans = 0;
  rep(i, N) ans += a.at(i);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
typedef long long ll;

int N, M, H, W, K, A, B;
string S;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  cin >> N >> M;
  vector<ll> a(N, 0);
  vector<pair<ll, ll>> cb(M, pair<ll, ll>(0, 0));
  rep(i, N) cin >> a.at(i);
  rep(i, M) cin >> cb.at(i).se >> cb.at(i).fi;
  sort(a.begin(), a.end());
  sort(cb.begin(), cb.end());
  reverse(cb.begin(), cb.end());
  int card = 0;
  bool fin = false;
  rep(i, M) {
    rep(j, cb.at(i).se) {
      if (card == N) {
        fin = true;
        break;
      }
      if (a.at(card) > cb.at(i).fi) {
        fin = true;
        break;
      }
      a.at(card) = cb.at(i).fi;
      card++;
    }
    if (fin)
      break;
  }
  ll ans = 0;
  rep(i, N) ans += a.at(i);
  cout << ans << endl;
}
Problem p03038 (C++)

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

struct c {
  int a;
  int b;

  bool operator<(const c &right) const { return b > right.b; }
};

int main() {
  long long int n, m, i, j, ima = 0, wa = 0;
  cin >> n >> m;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  vector<c> d(m);
  for (i = 0; i < m; i++) {
    cin >> d[i].a >> d[i].b;
  }
  sort(d.begin(), d.end());
  for (i = 0; i < m; i++) {
    for (;;) {
      if (a[ima] < d[i].b) {
        a[ima] = d[i].b;
      }
      d[i].a--;
      ima++;
      if (d[i].a == 0 || ima == n) {
        break;
      }
      if (ima == n) {
        break;
      }
    }
  }
  for (i = 0; i < n; i++) {
    wa += a[i];
  }
  cout << wa;
}

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

struct c {
  int a;
  int b;

  bool operator<(const c &right) const { return b > right.b; }
};

int main() {
  long long int n, m, i, j, ima = 0, wa = 0;
  cin >> n >> m;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  vector<c> d(m);
  for (i = 0; i < m; i++) {
    cin >> d[i].a >> d[i].b;
  }
  sort(d.begin(), d.end());
  for (i = 0; i < m; i++) {
    for (;;) {
      if (a[ima] < d[i].b) {
        a[ima] = d[i].b;
      }
      d[i].a--;
      ima++;
      if (d[i].a == 0 || ima == n) {
        break;
      }
    }
    if (ima == n) {
      break;
    }
  }
  for (i = 0; i < n; i++) {
    wa += a[i];
  }
  cout << wa;
}
Problem p03038 (C++)

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<pair<ll, ll>> B(M);
  REP(i, N) cin >> A.at(i);
  REP(i, M) {
    int x, y;
    cin >> x;
    cin >> y;
    B.at(i) = make_pair(y, x);
  }
  sort(B.rbegin(), B.rend());
  sort(A.begin(), A.end());
  int x = 0;
  bool ok = 0;
  REP(i, M) {
    REP(l, B.at(i).second) {
      if (A.at(x) < B.at(i).first) {
        A.at(x) = B.at(i).first;
        x++;
        if (x >= N)
          ok = 1;
      }
      if (ok == 1)
        break;
    }
    if (ok == 1)
      break;
  }
  ll answer = 0;
  REP(i, N) answer += A.at(i);
  cout << answer << "\n";
}


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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<pair<ll, ll>> B(M);
  REP(i, N) cin >> A.at(i);
  REP(i, M) {
    int x, y;
    cin >> x;
    cin >> y;
    B.at(i) = make_pair(y, x);
  }
  sort(B.rbegin(), B.rend());
  sort(A.begin(), A.end());
  int x = 0;
  bool ok = 0;
  REP(i, M) {
    REP(l, B.at(i).second) {
      if (A.at(x) < B.at(i).first) {
        A.at(x) = B.at(i).first;
        x++;
        if (x >= N)
          ok = 1;
      } else {
        ok = 1;
      }
      if (ok == 1)
        break;
    }
    if (ok == 1)
      break;
  }
  ll answer = 0;
  REP(i, N) answer += A.at(i);
  cout << answer << "\n";
}
Problem p03038 (C++)

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

int main() {
  int n, m;
  long b, c, temp = 9999999999, ans = 0;
  vector<pair<long, long>> p;

  cin >> n >> m;

  vector<long> a(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    temp = min(temp, a[i]);
  }

  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    p.push_back(make_pair(c, b));
  }

  sort(p.begin(), p.end());
  sort(a.begin(), a.end());

  long soe = 0;
  m--;

  while (p[m].first >= a[soe] && n >= 0) {
    while (p[m].first > a[soe] && p[m].second > 0) {
      a[soe] = p[m].first;
      p[m].second--;
      soe++;
    }
    m--;
  }

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

  cout << ans;

  return 0;
}

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

int main() {
  int n, m;
  long b, c, temp = 9999999999, ans = 0;
  vector<pair<long, long>> p;

  cin >> n >> m;

  vector<long> a(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    temp = min(temp, a[i]);
  }

  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    p.push_back(make_pair(c, b));
  }

  sort(p.begin(), p.end());
  sort(a.begin(), a.end());

  long soe = 0;
  m--;

  while (p[m].first >= a[soe] && m >= 0) {
    while (p[m].first > a[soe] && p[m].second > 0) {
      a[soe] = p[m].first;
      p[m].second--;
      soe++;
    }
    m--;
  }

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

  cout << ans;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fu(i, r, t) for (int i = r; i <= t; i++)
#define fd(i, r, t) for (int i = r; i >= t; i--)
#define Set set<int>
#define int long long
#define P pair<int, int>
const int maxn = 1e6;
const int INF = 1e18 + 6;
using namespace std;
int n, m, a[maxn], b[maxn], c[maxn];
// bool operator<(const P& a, const P& b) {
//     return a.parameter<b.parameter;
// }
priority_queue<P> T;
int32_t main() {
  IOS;
  cin >> n >> m;
  int sum = 0;
  fu(i, 1, n) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a + 1, a + n + 1);
  fu(i, 1, m) {
    cin >> b[i] >> c[i];
    T.push({c[i], b[i]});
  }
  int zuo = 0;
  int key = 0;
  while (!T.empty()) {
    auto h = T.top();
    T.pop();
    int C = h.first;
    int B = h.second;
    fu(i, 1, B) {
      if (a[++zuo] < C) {
        sum += C - a[zuo];
      } else {
        key = 1;
        break;
      }
    }
    if (key)
      break;
  }
  cout << sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fu(i, r, t) for (int i = r; i <= t; i++)
#define fd(i, r, t) for (int i = r; i >= t; i--)
#define Set set<int>
#define int long long
#define P pair<int, int>
const int maxn = 1e6;
const int INF = 1e18 + 6;
using namespace std;
int n, m, a[maxn], b[maxn], c[maxn];
// bool operator<(const P& a, const P& b) {
//     return a.parameter<b.parameter;
// }
priority_queue<P> T;
int32_t main() {
  IOS;
  cin >> n >> m;
  int sum = 0;
  fu(i, 1, n) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a + 1, a + n + 1);
  fu(i, 1, m) {
    cin >> b[i] >> c[i];
    T.push({c[i], b[i]});
  }
  int zuo = 0;
  int key = 0;
  while (!T.empty()) {
    auto h = T.top();
    T.pop();
    int C = h.first;
    int B = h.second;
    fu(i, 1, B) {
      if (a[++zuo] < C && zuo <= n) {
        sum += C - a[zuo];
      } else {
        key = 1;
        break;
      }
    }
    if (key)
      break;
  }
  cout << sum;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long int;
template <typename T> void swap(T *t1, T *t2) {
  T *tmp = t1;
  t1 = t2;
  t2 = tmp;
}
Int tmpi = 0;
double tmpd = 0.0;

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

  Int n, m;
  vector<Int> a;
  multimap<Int, Int, greater<Int>> bc;
  cin >> n >> m;
  for (Int i = 0; i < n; i++) {
    cin >> tmpi;
    a.push_back(tmpi);
  }

  Int tmpi2;
  for (Int i = 0; i < m; i++) {
    cin >> tmpi >> tmpi2;
    bc.insert(make_pair(tmpi2, tmpi));
  }
  sort(a.begin(), a.end());
  Int it = 0;
  Int sum = 0;
  Int cnt = 0;
  for (const auto &u : bc) {
    Int num = u.first;
    Int len = u.second;
    bool flg = false;
    Int maxlen = it + len - 1 < n ? it + len - 1 : n;
    for (Int i = it + len - 1; i >= it; i--) {
      cnt++;
      if (a[i] < num) {
        sum += num * (i - it + 1);
        it = i + 1;
        if (cnt != bc.size()) {
          flg = true;
        }
        break;
      }
    }
    if (!flg) {
      for (Int i = it; i < n; i++) {
        sum += a[i];
      }
      break;
    }
  }

  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long int;
template <typename T> void swap(T *t1, T *t2) {
  T *tmp = t1;
  t1 = t2;
  t2 = tmp;
}
Int tmpi = 0;
double tmpd = 0.0;

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

  Int n, m;
  vector<Int> a;
  multimap<Int, Int, greater<Int>> bc;
  cin >> n >> m;
  for (Int i = 0; i < n; i++) {
    cin >> tmpi;
    a.push_back(tmpi);
  }

  Int tmpi2;
  for (Int i = 0; i < m; i++) {
    cin >> tmpi >> tmpi2;
    bc.insert(make_pair(tmpi2, tmpi));
  }
  sort(a.begin(), a.end());
  Int it = 0;
  Int sum = 0;
  Int cnt = 0;
  for (const auto &u : bc) {
    Int num = u.first;
    Int len = u.second;
    bool flg = false;
    Int maxlen = it + len < n ? it + len : n;
    for (Int i = maxlen - 1; i >= it; i--) {
      cnt++;
      if (a[i] < num) {
        sum += num * (i - it + 1);
        it = i + 1;
        if (cnt != bc.size()) {
          flg = true;
        }
        break;
      }
    }
    if (!flg) {
      for (Int i = it; i < n; i++) {
        sum += a[i];
      }
      break;
    }
  }

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

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

using namespace std;

using ll = long long;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  vector<pair<ll, ll>> nikola;
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  for (ll i = 0; i < m; ++i) {
    ll b, c;
    cin >> b >> c;
    nikola.push_back(make_pair(c, b));
  }
  sort(nikola.begin(), nikola.end());
  reverse(nikola.begin(), nikola.end());
  ll ans = 0, cur = 0;
  for (ll i = 0; i < n; ++i) {
    if (nikola[cur].first > v[i] && nikola[cur].second > 0) {
      ans += nikola[cur].first;
      nikola[cur].second--;
    } else if (nikola[cur].second == 0) {
      cur++;
      if (i == n - 1) {
        ans += v[i];
        break;
      }
      i--;
    } else {
      ans += v[i];
    }
  }
  cout << ans;
}


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

using namespace std;

using ll = long long;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  vector<pair<ll, ll>> nikola;
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  for (ll i = 0; i < m; ++i) {
    ll b, c;
    cin >> b >> c;
    nikola.push_back(make_pair(c, b));
  }
  sort(nikola.begin(), nikola.end());
  reverse(nikola.begin(), nikola.end());
  ll ans = 0, cur = 0;
  for (ll i = 0; i < n; ++i) {
    if (nikola[cur].first > v[i] && nikola[cur].second > 0) {
      ans += nikola[cur].first;
      nikola[cur].second--;
    } else if (nikola[cur].second == 0 && cur < m) {
      cur++;
      if (i == n - 1) {
        ans += v[i];
        break;
      }
      i--;
    } else {
      ans += v[i];
    }
  }
  cout << ans;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
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];
  }
  sort(a.begin(), a.end());
  vector<pair<int, int>> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i].first >> x[i].second;
  }
  sort(x.begin(), x.end(),
       [](const pair<int, int> &p, const pair<int, int> &q) {
         return p.second > q.second;
       });
  vector<int> d;
  bool fill = false;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < x[i].first; j++) {
      d.push_back(x[i].second);
      if (d.size() == n) {
        fill = true;
        break;
      }
    }
    if (fill) {
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (d[i] >= a[i]) {
      a[i] = d[i];
    }
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
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];
  }
  sort(a.begin(), a.end());
  vector<pair<int, int>> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i].first >> x[i].second;
  }
  sort(x.begin(), x.end(),
       [](const pair<int, int> &p, const pair<int, int> &q) {
         return p.second > q.second;
       });
  vector<int> d;
  bool fill = false;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < x[i].first; j++) {
      d.push_back(x[i].second);
      if (d.size() == n) {
        fill = true;
        break;
      }
    }
    if (fill) {
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (d.size() > i && d[i] >= a[i]) {
      a[i] = d[i];
    }
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef pair<int, int> pii;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int INF = 987654321;
const ll MOD = 1000000007LL;

ll ipow(ll base, ll exp) {
  ll result = 1LL;
  for (;;) {
    if (exp & 1)
      result *= base;
    exp >>= 1;
    if (!exp)
      break;
    base *= base;
  }

  return result;
}

// const long double PI = (acos(-1));
// const long double EPS = 0.0000000001;

int n, m;
vi a;
map<ll, ll> record;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt", "r", stdin);
  cin >> n >> m;
  a.resize(n);
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) {
    ll b, c;
    cin >> b >> c;
    if (record.find(c) != record.end()) {
      record[c] += b;
      record[c] = max(record[c], (ll)n);
    } else {
      record[c] = b;
    }
  }

  ll cnt = n;
  auto it = record.rbegin();
  while (cnt > 0 && it != record.rend()) {
    ll b = it->S;
    ll c = it->F;
    // cout << b << " : " << c << endl;
    for (int i = 0; i < b; ++i) {
      a.pb(c);
    }
    cnt -= b;
    it++;
  }

  sort(all(a));

  ll total = 0;
  per(i, sz(a) - n, sz(a)) { total += (ll)a[i]; }

  cout << total << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef pair<int, int> pii;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int INF = 987654321;
const ll MOD = 1000000007LL;

ll ipow(ll base, ll exp) {
  ll result = 1LL;
  for (;;) {
    if (exp & 1)
      result *= base;
    exp >>= 1;
    if (!exp)
      break;
    base *= base;
  }

  return result;
}

// const long double PI = (acos(-1));
// const long double EPS = 0.0000000001;

int n, m;
vi a;
map<ll, ll> record;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt", "r", stdin);
  cin >> n >> m;
  a.resize(n);
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) {
    ll b, c;
    cin >> b >> c;
    if (record.find(c) != record.end()) {
      record[c] += b;
      record[c] = min(record[c], (ll)n);
    } else {
      record[c] = b;
    }
  }

  ll cnt = n;
  auto it = record.rbegin();
  while (cnt > 0 && it != record.rend()) {
    ll b = it->S;
    ll c = it->F;
    // cout << b << " : " << c << endl;
    for (int i = 0; i < b; ++i) {
      a.pb(c);
    }
    cnt -= b;
    it++;
  }

  sort(all(a));

  ll total = 0;
  per(i, sz(a) - n, sz(a)) { total += (ll)a[i]; }

  cout << total << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\n'

typedef long long ll;
typedef pair<ll, ll> pii;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll sum(vector<ll> &a) {
  ll ans = 0;
  for (auto &x : a)
    ans += x;
  return ans;
}

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, m;
  cin >> n >> m;

  vector<ll> a(n);

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

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

  vector<pii> ops;

  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    ops.push_back({c, b});
  }

  sort(ops.rbegin(), ops.rend());

  int lo = 0;
  for (int i = 0; i < m && lo < n; i++) {

    for (ll j = 0; j < ops[i].second && lo < n; j++) {
      if (ops[i].first > a[lo]) {
        a[lo] = ops[i].first;
        lo++;
      }
    }
  }

  cout << sum(a) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\n'

typedef long long ll;
typedef pair<ll, ll> pii;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll sum(vector<ll> &a) {
  ll ans = 0;
  for (auto &x : a)
    ans += x;
  return ans;
}

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, m;
  cin >> n >> m;

  vector<ll> a(n);

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

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

  vector<pii> ops;

  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    ops.push_back({c, b});
  }

  sort(ops.rbegin(), ops.rend());

  int lo = 0;
  for (int i = 0; i < m && lo < n; i++) {

    for (ll j = 0; j < ops[i].second && lo < n; j++) {
      if (ops[i].first > a[lo]) {
        a[lo] = ops[i].first;
        lo++;
      } else
        lo++, j--;
    }
  }

  cout << sum(a) << endl;

  return 0;
}
Problem p03038 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007      // 10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<tuple<ll, int>> itm;
  REP(i, n)
  cin >> a[i];
  REP(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    itm.emplace_back(c, b);
  }

  sort(ALL(a));
  sort(ALL(itm), greater<tuple<ll, int>>());

  ll vec_idx = 0;
  REP(i, SIZE(itm)) {
    // cが新しい数字, bが変更できる数
    ll c;
    int b;
    tie(c, b) = itm[i];
    REP(j, b) {
      if (a[vec_idx] < c) {
        a[vec_idx] = c;
        vec_idx++;
      }
    }
  }

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


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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007      // 10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<tuple<ll, int>> itm;
  REP(i, n)
  cin >> a[i];
  REP(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    itm.emplace_back(c, b);
  }

  sort(ALL(a));
  sort(ALL(itm), greater<tuple<ll, int>>());

  ll vec_idx = 0;
  REP(i, SIZE(itm)) {
    // cが新しい数字, bが変更できる数
    ll c;
    int b;
    tie(c, b) = itm[i];
    REP(j, b) {
      if (a[vec_idx] < c) {
        a[vec_idx] = c;
        vec_idx++;
      } else
        break;
    }
  }

  ll ans = 0;
  REP(i, n)
  ans += a[i];
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> BC(M);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> BC[i].first >> BC[i].second;
  }
  sort(BC.begin(), BC.end(),
       [](auto l, auto r) { return l.second > r.second; });
  sort(A.begin(), A.end(), [](auto l, auto r) { return l < r; });
  int left = 0;
  for (int i = 0; i < M; ++i) {
    int count = 0;
    for (int j = left; j < N; ++j) {
      if (A[j] < BC[i].second) {
        A[j] = BC[i].second;
        ++left;
        ++count;
      }
      if (count >= BC[i].first)
        break;
    }
  }
  long long s = 0;
  for (int i = 0; i < N; ++i) {
    s += A[i];
  }
  cout << s << endl;
}


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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> BC(M);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> BC[i].first >> BC[i].second;
  }
  sort(BC.begin(), BC.end(),
       [](auto l, auto r) { return l.second > r.second; });
  sort(A.begin(), A.end(), [](auto l, auto r) { return l < r; });
  int left = 0;
  for (int i = 0; i < M; ++i) {
    int count = 0;
    for (int j = left; j < N; ++j) {
      if (A[j] < BC[i].second) {
        A[j] = BC[i].second;
        ++left;
        ++count;
      } else
        break;
      if (count >= BC[i].first)
        break;
    }
  }
  long long s = 0;
  for (int i = 0; i < N; ++i) {
    s += A[i];
  }
  cout << s << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int64_t N, M;
  cin >> N >> M;

  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());

  auto compare = [](pair<int64_t, int64_t> b, pair<int64_t, int64_t> c) {
    return b.second < c.second;
  };

  priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                 decltype(compare)>
      BC{compare};

  for (int64_t i = 0; i < M; i++) {
    int64_t tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    BC.push(make_pair(tmp_b, tmp_c));
  }

  int64_t sum = 0;
  int64_t c_pos = 0;
  while (BC.size() > 0) {
    auto tmp = BC.top();
    BC.pop();
    for (int64_t i = 0; i < tmp.first and c_pos < A.size(); i++) {
      if (A.at(c_pos) < tmp.second) {
        A.at(c_pos) = tmp.second;
        c_pos++;
      }
    }
  }
  for (int64_t i = 0; i < A.size(); i++) {
    sum += A.at(i);
  }

  cout << sum;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int64_t N, M;
  cin >> N >> M;

  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());

  auto compare = [](pair<int64_t, int64_t> b, pair<int64_t, int64_t> c) {
    return b.second < c.second;
  };

  priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                 decltype(compare)>
      BC{compare};

  for (int64_t i = 0; i < M; i++) {
    int64_t tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    BC.push(make_pair(tmp_b, tmp_c));
  }

  int64_t sum = 0;
  int64_t c_pos = 0;
  while (BC.size() > 0) {
    auto tmp = BC.top();
    BC.pop();
    for (int64_t i = 0; i < tmp.first and c_pos < A.size(); i++) {
      if (A.at(c_pos) < tmp.second) {
        A.at(c_pos) = tmp.second;
        c_pos++;
      } else {
        break;
      }
    }
  }
  for (int64_t i = 0; i < A.size(); i++) {
    sum += A.at(i);
  }

  cout << sum;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<V> VV;
// typedef vector<VV> VVV;
using namespace std;

int main() {
  ll n, m, s, t, a, b, c;
  vector<ll> vec;

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a;
    vec.push_back(a);
  }
  sort(vec.begin(), vec.end());

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

  for (ll i = 0; i < m; i++) {
    cin >> b >> c;
    pairs[i] = make_pair(c, b);
  }

  // firstが小さい順、secondが小さい順にソート
  sort(pairs.begin(), pairs.end());

  ll flag = 1, index = 0, index2 = m - 1;
  while (index2 != -1) {
    if (vec[index] > pairs[index2].first || index >= n) {
      break;
    }
    if (vec[index] < pairs[index2].first) {
      vec[index] = pairs[index2].first;
      index++;
      pairs[index2].second--;
    }
    if (pairs[index2].second <= 0)
      index2--;
  }
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    sum += vec[i];
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<V> VV;
// typedef vector<VV> VVV;
using namespace std;

int main() {
  ll n, m, s, t, a, b, c;
  vector<ll> vec;

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a;
    vec.push_back(a);
  }
  sort(vec.begin(), vec.end());

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

  for (ll i = 0; i < m; i++) {
    cin >> b >> c;
    pairs[i] = make_pair(c, b);
  }

  // firstが小さい順、secondが小さい順にソート
  sort(pairs.begin(), pairs.end());

  ll flag = 1, index = 0, index2 = m - 1;
  while (index2 != -1) {
    if (vec[index] > pairs[index2].first || index >= n) {
      break;
    }
    if (vec[index] < pairs[index2].first) {
      vec[index] = pairs[index2].first;
      index++;
      pairs[index2].second--;
    } else {
      break;
    }
    if (pairs[index2].second <= 0)
      index2--;
  }
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    sum += vec[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;
typedef pair<ll, ll> P;
ll Mod = 1000000007;
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<P> Card(M);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll sum = 0;
  for (int i = 0; i < M; i++) {
    cin >> Card[i].second >> Card[i].first;
    sum += Card[i].second;
  }
  sort(Card.begin(), Card.end());
  reverse(Card.begin(), Card.end());
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    A.push_back(Card[cnt].first);
    Card[cnt].second--;
    if (Card[cnt].second == 0) {
      cnt++;
    }
    if (i == sum) {
      break;
    }
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll 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;
using ll = long long;
typedef pair<ll, ll> P;
ll Mod = 1000000007;
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<P> Card(M);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll sum = 0;
  for (int i = 0; i < M; i++) {
    cin >> Card[i].second >> Card[i].first;
    sum += Card[i].second;
  }
  sort(Card.begin(), Card.end());
  reverse(Card.begin(), Card.end());
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    A.push_back(Card[cnt].first);
    Card[cnt].second--;
    if (Card[cnt].second == 0) {
      cnt++;
    }
    if (i == sum - 1) {
      break;
    }
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 100001;
int n, m;
int a[maxn];

struct bbb {
  int t;
  int num;
};
bbb b[maxn];

bool cmp(bbb x, bbb y) { return x.num > y.num; }

int main() {
  long long ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].t >> b[i].num;
  }
  sort(a, a + n);
  sort(b, b + m, cmp);
  int c = 0;
  for (int i = 0; i < m; i++) {
    int cc = c;
    for (int j = cc; j < b[i].t + cc; j++) {
      if (j == n)
        break;
      if (a[j] < b[i].num) {
        ans += b[i].num - a[j];
        c = j + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 100001;
int n, m;
int a[maxn];

struct bbb {
  int t;
  int num;
};
bbb b[maxn];

bool cmp(bbb x, bbb y) { return x.num > y.num; }

int main() {
  long long ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].t >> b[i].num;
  }
  sort(a, a + n);
  sort(b, b + m, cmp);
  int c = 0;
  for (int i = 0; i < m; i++) {
    int cc = c;
    for (int j = cc; j < b[i].t + cc; j++) {
      if (j == n)
        break;
      if (a[j] < b[i].num) {
        ans += b[i].num - a[j];
        c = j + 1;
      } else
        break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

#define INF 2000000007
#define MOD 998244353
#define MAX 100005

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

template <typename T> void print(vector<T>);

struct S {
  int b;
  int c;
  S() { b = 0, c = 0; }
  S(int x1, int x2) {
    b = x1;
    c = x2;
  }
  bool operator<(const S &another) const { return c < another.c; };
  bool operator>(const S &another) const { return c > another.c; };
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<S> BC(M);
  REP(i, N) cin >> A[i];
  sort(A.begin(), A.end());

  REP(i, M) {
    int B, C;
    cin >> B >> C;
    BC[i].b = B;
    BC[i].c = C;
  }
  sort(BC.begin(), BC.end(), greater<S>());

  int itr = 0;
  REP(i, M) {
    REP(j, BC[i].b) {
      if (BC[i].c > A[itr])
        A[itr] = BC[i].c;
      itr++;
    }
  }

  // print(A);

  ull cnt = 0;
  REP(i, N) cnt += A[i];
  cout << cnt << endl;

  return 0;
}

template <typename T> void print(vector<T> vec) {
  cout << "[";
  for (auto itr : vec)
    cout << itr << ":";
  cout << "]" << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

#define INF 2000000007
#define MOD 998244353
#define MAX 100005

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

template <typename T> void print(vector<T>);

struct S {
  int b;
  int c;
  S() { b = 0, c = 0; }
  S(int x1, int x2) {
    b = x1;
    c = x2;
  }
  bool operator<(const S &another) const { return c < another.c; };
  bool operator>(const S &another) const { return c > another.c; };
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<S> BC(M);
  REP(i, N) cin >> A[i];
  sort(A.begin(), A.end());

  REP(i, M) {
    int B, C;
    cin >> B >> C;
    BC[i].b = B;
    BC[i].c = C;
  }
  sort(BC.begin(), BC.end(), greater<S>());

  int itr = 0;
  REP(i, M) {
    REP(j, BC[i].b) {
      if (itr > N)
        break;
      if (BC[i].c > A[itr])
        A[itr] = BC[i].c;
      itr++;
    }
  }

  // print(A);

  ull cnt = 0;
  REP(i, N) cnt += A[i];
  cout << cnt << endl;

  return 0;
}

template <typename T> void print(vector<T> vec) {
  cout << "[";
  for (auto itr : vec)
    cout << itr << ":";
  cout << "]" << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
static const uint64_t MOD = 1000000007LL;
// uint64_t dp[100005][13];

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

  map<long, long> mcb;
  priority_queue<long, vector<long>, greater<long>> q;
  for (int i = 0; i < n; i++) {
    long a;
    cin >> a;
    q.push(a);
  }
  for (int i = 0; i < m; i++) {
    long b, c;
    cin >> b >> c;
    mcb[c] += b;
  }

  for (auto e : mcb) {
    long c = e.first;
    long b = e.second;
    for (int i = 0; i < b; i++) {
      if (q.top() < c) {
        q.pop();
        q.push(c);
      } else {
        break;
      }
    }
  }

  long ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
static const uint64_t MOD = 1000000007LL;
// uint64_t dp[100005][13];

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

  map<long, long, greater<long>> mcb;
  priority_queue<long, vector<long>, greater<long>> q;
  for (int i = 0; i < n; i++) {
    long a;
    cin >> a;
    q.push(a);
  }
  for (int i = 0; i < m; i++) {
    long b, c;
    cin >> b >> c;
    mcb[c] += b;
  }

  for (auto e : mcb) {
    long c = e.first;
    long b = e.second;
    for (int i = 0; i < b; i++) {
      if (q.top() < c) {
        q.pop();
        q.push(c);
      } else {
        break;
      }
    }
  }

  long ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }

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

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

using namespace std;

typedef pair<long, long> P;

int main() {
  long n, m;
  long ans = 0;
  cin >> n >> m;
  vector<long> a(n);
  priority_queue<P> pq;
  for (long i = 0; i < n; i++)
    cin >> a[i];
  for (long i = 0; i < m; i++) {
    long b, c;
    cin >> b >> c;
    pq.push(P(c, b));
  }

  sort(a.begin(), a.end());
  for (long i = 0; i < n; i++) {
    long b, c;
    tie(c, b) = pq.empty() ? P(-1, -1) : pq.top();
    pq.pop();
    if (c <= a[i]) {
      pq.push(P(c, b));
      ans += a[i];
      continue;
    } else {
      ans += c;
      if (b - 1 > 0)
        pq.push(P(c, b - 1));
    }
  }

  cout << ans << endl;
}

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

using namespace std;

typedef pair<long, long> P;

int main() {
  long n, m;
  long ans = 0;
  cin >> n >> m;
  vector<long> a(n);
  priority_queue<P> pq;
  for (long i = 0; i < n; i++)
    cin >> a[i];
  for (long i = 0; i < m; i++) {
    long b, c;
    cin >> b >> c;
    pq.push(P(c, b));
  }

  sort(a.begin(), a.end());
  for (long i = 0; i < n; i++) {
    long b, c;
    if (pq.empty()) {
      b = c = -1;
    } else {
      tie(c, b) = pq.top();
      pq.pop();
    }
    if (c <= a[i]) {
      pq.push(P(c, b));
      ans += a[i];
      continue;
    } else {
      ans += c;
      if (b - 1 > 0)
        pq.push(P(c, b - 1));
    }
  }

  cout << ans << endl;
}