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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  map<ll, int> A;
  for (int i = 0; i < N; i++) {
    ll a;
    scanf("%lld", &a);
    A[a]++;
  }
  vector<int> B(N);
  vector<ll> C(N);
  for (int i = 0; i < M; i++) {
    scanf("%d%lld", &B[i], &C[i]);
  }
  // for (auto k = A.begin(); k != A.end(); ++k)
  //   for (int l = 0; l < k->second; l++)
  //     printf("%lld ", k->first);
  // printf("\n");
  for (int i = 0; i < M; i++) {
    int b = B[i];
    while (b > 0) {
      auto j = A.begin();
      if (j == A.end() || j->first >= C[i])
        break;
      int p = min(b, j->second);
      ll key = j->first;
      int q = j->second - p;
      A[C[i]] += p;
      if (q == 0) {
        A.erase(key);
      } else {
        A[key] = q;
      }
      b -= p;
    }
    // for (auto k = A.begin(); k != A.end(); ++k)
    //   for (int l = 0; l < k->second; l++)
    //     printf("%lld ", k->first);
    // printf("\n");
  }
  ll ans = 0;
  for (auto k = A.begin(); k != A.end(); ++k) {
    ans += k->first * k->second;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  map<ll, int> A;
  for (int i = 0; i < N; i++) {
    ll a;
    scanf("%lld", &a);
    A[a]++;
  }
  vector<int> B(M);
  vector<ll> C(M);
  for (int i = 0; i < M; i++) {
    scanf("%d%lld", &B[i], &C[i]);
  }
  // for (auto k = A.begin(); k != A.end(); ++k)
  //   for (int l = 0; l < k->second; l++)
  //     printf("%lld ", k->first);
  // printf("\n");
  for (int i = 0; i < M; i++) {
    int b = B[i];
    while (b > 0) {
      auto j = A.begin();
      if (j == A.end() || j->first >= C[i])
        break;
      int p = min(b, j->second);
      ll key = j->first;
      int q = j->second - p;
      A[C[i]] += p;
      if (q == 0) {
        A.erase(key);
      } else {
        A[key] = q;
      }
      b -= p;
    }
    // for (auto k = A.begin(); k != A.end(); ++k)
    //   for (int l = 0; l < k->second; l++)
    //     printf("%lld ", k->first);
    // printf("\n");
  }
  ll ans = 0;
  for (auto k = A.begin(); k != A.end(); ++k) {
    ans += k->first * k->second;
  }
  printf("%lld\n", ans);
}
Problem p03038 (C++)

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

bool f(pair<ll, ll> p1, pair<ll, ll> p2) {
  if (p1.second == p2.second)
    return p1.first > p2.first;
  return p1.second > p2.second;
}

int main() {
  ll n, m, i, j;
  cin >> n >> m;
  ll a[n];
  vector<pair<ll, ll>> b(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
  }

  sort(a, a + n);
  sort(b.begin(), b.end(), f);

  for (i = 0, j = 0; i < n && j < m; i++) {
    if (a[i] < b[j].second) {
      a[i] = b[j].second;
      b[j].first--;
      if (b[j].first == 0)
        j++;
    } else
      break;
  }
  ll sum = 0;
  for (i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}


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

bool f(pair<ll, ll> p1, pair<ll, ll> p2) {
  if (p1.second == p2.second)
    return p1.first > p2.first;
  return p1.second > p2.second;
}

int main() {
  ll n, m, i, j;
  cin >> n >> m;
  ll a[n];
  vector<pair<ll, ll>> b(m);
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
  }

  sort(a, a + n);
  sort(b.begin(), b.end(), f);

  for (i = 0, j = 0; i < n && j < m; i++) {
    if (a[i] < b[j].second) {
      a[i] = b[j].second;
      b[j].first--;
      if (b[j].first == 0)
        j++;
    } else
      break;
  }
  ll sum = 0;
  for (i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

BC.sort(reverse=True, key=lambda x: x[1])

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A[i:], bc[1]) + i
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))


Fixed submission:
import bisect

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

BC.sort(reverse=True, key=lambda x: x[1])

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A, bc[1])
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  vector<pair<ll, ll>> b(m);
  rep(i, m) { cin >> b[i].second >> b[i].first; }
  sort(b.begin(), b.end());
  /*rep(i, m){
      cout << b[i].first << " " << b[i].second << endl;
  }*/

  vector<ll> a_sum(n + 1);
  a_sum[0] = 0;
  _rep(i, 1, n + 1) { a_sum[i] = a_sum[i - 1] + a[i - 1]; }
  ll num = 0;
  ll c_num = 0;
  ll ans = a_sum[n];
  while (true) {
    if (a[num] < b[m - 1].first) {
      ans += b[m - 1].first - a[num];
    }
    c_num++;
    num++;
    if (c_num == b[m - 1].second) {
      m--, c_num = 0;
    }

    if (num == n - 1 || (m == 1 && c_num == b[0].second))
      break;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  vector<pair<ll, ll>> b(m);
  rep(i, m) { cin >> b[i].second >> b[i].first; }
  sort(b.begin(), b.end());
  /*rep(i, m){
      cout << b[i].first << " " << b[i].second << endl;
  }*/

  vector<ll> a_sum(n + 1);
  a_sum[0] = 0;
  _rep(i, 1, n + 1) { a_sum[i] = a_sum[i - 1] + a[i - 1]; }
  ll num = 0;
  ll c_num = 0;
  ll ans = a_sum[n];
  while (true) {
    if (a[num] < b[m - 1].first) {
      ans += b[m - 1].first - a[num];
    }
    c_num++;
    num++;
    if (c_num == b[m - 1].second) {
      m--, c_num = 0;
    }

    if (num == n || (m == 1 && c_num == b[0].second))
      break;
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

int N, M, A[100001], B[100001], C[100001];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i] >> C[i];

  sort(A, A + N);

  vector<pair<int, int>> CB(M);
  for (int i = 0; i < M; i++)
    CB[i] = make_pair(C[i], B[i]);
  sort(CB.begin(), CB.end(), greater<pair<int, int>>());

  int k = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < CB[i].second; j++) {
      if (N <= k)
        break;
      if (A[k] < CB[i].first) {
        A[k] = CB[i].first;
        k++;
      }
    }
    if (N <= k)
      break;
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];

  cout << ans << endl;
}

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

int N, M, A[100001], B[100001], C[100001];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i] >> C[i];

  sort(A, A + N);

  vector<pair<int, int>> CB(M);
  for (int i = 0; i < M; i++)
    CB[i] = make_pair(C[i], B[i]);
  sort(CB.begin(), CB.end(), greater<pair<int, int>>());

  int k = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < CB[i].second; j++) {
      if (N <= k)
        break;
      if (A[k] < CB[i].first) {
        A[k] = CB[i].first;
        k++;
      } else
        break;
    }
    if (N <= k)
      break;
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  pair<ll, ll> p[n];
  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    p[i] = make_pair(c, b);
  }
  sort(a, a + n);
  sort(p, p + m, greater<pair<ll, ll>>());

  ll curr = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] >= p[curr].first)
      break;
    a[i] = p[curr].first;
    cnt++;
    if (cnt == p[curr].second) {
      curr++;
      cnt = 0;
      if (curr == m) {
        break;
      }
    }
  }
  ll sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  pair<ll, ll> p[m];
  for (int i = 0; i < m; i++) {
    ll b, c;
    cin >> b >> c;
    p[i] = make_pair(c, b);
  }
  sort(a, a + n);
  sort(p, p + m, greater<pair<ll, ll>>());

  ll curr = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] >= p[curr].first)
      break;
    a[i] = p[curr].first;
    cnt++;
    if (cnt == p[curr].second) {
      curr++;
      cnt = 0;
      if (curr == m) {
        break;
      }
    }
  }
  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):
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define INF 1e9
#define INFLL 1e18
#define EPS 1e-9
#define MOD 1000000007
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define REPR1(i, n) for (ll i = (n); i > 0; --i)
#define REPC(i, obj) for (auto i : obj)
#define ALL(obj) (obj).begin(), (obj).end()
#define SETP(n) cout << fixed << setprecision(n)
#define VV(T, h, w) vector<vector<T>>(h, vector<T>(w))
#define VVI(T, h, w, i) vector<vector<T>>(h, vector<T>(w, i))
using namespace std;
using ll = unsigned long long;
template <typename T = ll> inline T in() {
  T ret;
  cin >> ret;
  return ret;
}

int main() {
  ll n = in(), m = in();

  vector<ll> a(n);
  REP(i, n) scanf("%lld", &a[i]);
  sort(ALL(a));

  vector<pair<ll, ll>> bc(m);
  REP(i, m) {
    ll b, c;
    scanf("%lld%lld", &b, &c);
    bc[i] = make_pair(c, b);
  }
  sort(ALL(bc), greater<pair<ll, ll>>());

  ll inow = 0, ans = 0;
  REP(i, m) {
    ll b = bc[i].second, c = bc[i].first;
    for (ll j = 0; j < min(b, n - inow); ++j) {
      ans += max(a[j + inow], c);
    }
    inow += b;
  }
  for (ll j = inow; j < n; ++j)
    ans += a[j];

  cout << ans << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define INF 1e9
#define INFLL 1e18
#define EPS 1e-9
#define MOD 1000000007
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define REPR1(i, n) for (ll i = (n); i > 0; --i)
#define REPC(i, obj) for (auto i : obj)
#define ALL(obj) (obj).begin(), (obj).end()
#define SETP(n) cout << fixed << setprecision(n)
#define VV(T, h, w) vector<vector<T>>(h, vector<T>(w))
#define VVI(T, h, w, i) vector<vector<T>>(h, vector<T>(w, i))
using namespace std;
using ll = long long;
template <typename T = ll> inline T in() {
  T ret;
  cin >> ret;
  return ret;
}

int main() {
  ll n = in(), m = in();

  vector<ll> a(n);
  REP(i, n) scanf("%lld", &a[i]);
  sort(ALL(a));

  vector<pair<ll, ll>> bc(m);
  REP(i, m) {
    ll b, c;
    scanf("%lld%lld", &b, &c);
    bc[i] = make_pair(c, b);
  }
  sort(ALL(bc), greater<pair<ll, ll>>());

  ll inow = 0, ans = 0;
  REP(i, m) {
    ll b = bc[i].second, c = bc[i].first;
    for (ll j = 0; j < min(b, n - inow); ++j) {
      ans += max(a[j + inow], c);
    }
    inow += b;
  }
  for (ll j = inow; j < n; ++j)
    ans += a[j];

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

#define ll long long

using namespace std;

long long gcd(int x, int y) {
  if (x == 0)
    return y;

  return gcd(y % x, x);
}

long long lcm(int x, int y) {
  if (y == 0) {
    return x;
  }

  return x * y / gcd(x, y);
}

ll min(ll a, ll b) { return a > b ? b : a; }

ll max(ll a, ll b) { return a > b ? a : b; }

int main() {
  cin.sync_with_stdio(false);

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

  priority_queue<ll, vector<ll>, greater<ll>> q;
  map<ll, ll> rep;
  vector<pair<ll, ll>> arr(m);

  for (int i = 0; i < n; i++) {
    ll qq;
    cin >> qq;

    q.push(qq);
    rep[qq]++;
  }

  for (int i = 0; i < m; i++) {
    // ll a,b;
    // cin>>a>>b;
    cin >> arr[i].second >> arr[i].first;

    // arr.push_back({b, a});
  }

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

  for (int i = 0; i < m; i++) {
    ll a = arr[i].second;
    ll b = arr[i].first;
    while (a > 0) {
      ll qq = q.top();
      // q.pop();
      if (qq >= b) {
        break;
      } else {
        ll smanji = min(rep[qq], a);
        rep[qq] -= smanji;
        rep[b] += smanji;
        a -= smanji;
        q.push(b);

        if (rep[qq] == 0) {
          q.pop();
          rep.erase(qq);
        }
      }
    }
  }

  ll res = 0;
  for (auto i : rep) {
    res += i.first * i.second;
  }

  cout << res << endl;
}

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

#define ll long long

using namespace std;

long long gcd(int x, int y) {
  if (x == 0)
    return y;

  return gcd(y % x, x);
}

long long lcm(int x, int y) {
  if (y == 0) {
    return x;
  }

  return x * y / gcd(x, y);
}

ll min(ll a, ll b) { return a > b ? b : a; }

ll max(ll a, ll b) { return a > b ? a : b; }

int main() {
  cin.sync_with_stdio(false);

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

  priority_queue<ll, vector<ll>, greater<ll>> q;
  map<ll, ll> rep;
  vector<pair<ll, ll>> arr(m);

  for (int i = 0; i < n; i++) {
    ll qq;
    cin >> qq;

    q.push(qq);
    rep[qq]++;
  }

  for (int i = 0; i < m; i++) {
    // ll a,b;
    // cin>>a>>b;
    cin >> arr[i].second >> arr[i].first;

    // arr.push_back({b, a});
  }

  sort(arr.begin(), arr.end());
  reverse(arr.begin(), arr.end());

  for (int i = 0; i < m; i++) {
    ll a = arr[i].second;
    ll b = arr[i].first;
    while (a > 0) {
      ll qq = q.top();
      // q.pop();
      if (qq >= b) {
        break;
      } else {
        ll smanji = min(rep[qq], a);
        rep[qq] -= smanji;
        rep[b] += smanji;
        a -= smanji;
        q.push(b);

        if (rep[qq] == 0) {
          q.pop();
          rep.erase(qq);
        }
      }
    }
  }

  ll res = 0;
  for (auto i : rep) {
    res += i.first * i.second;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<pair<ll, ll>> b(m);
  rep(i, m) { cin >> b[i].second >> b[i].first; }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end(), greater<>());
  // rep(i, m) cout << b[i].first << " " << b[i].second << endl;

  int i = 0;
  rep(j, m) {
    rep(k, b[j].second) {
      if (i + k >= n)
        break;
      if (a[i + k] < b[j].first) {
        a[i + k] = b[j].first;
      }
    }
    i += b[j].second;
  }
  ll sum = 0;
  rep(i, n) { sum += a[i]; }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<pair<ll, ll>> b(m);
  rep(i, m) { cin >> b[i].second >> b[i].first; }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end(), greater<>());
  // rep(i, m) cout << b[i].first << " " << b[i].second << endl;

  int i = 0;
  rep(j, m) {
    if (i >= n)
      break;
    rep(k, b[j].second) {
      if (i + k >= n)
        break;
      if (a[i + k] < b[j].first) {
        a[i + k] = b[j].first;
      }
    }
    i += b[j].second;
  }
  ll sum = 0;
  rep(i, n) { sum += a[i]; }
  cout << sum << endl;
}
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    CB.append((c, b))

# ascend
A.sort()

# descend
CB.sort(reverse=True)

# fill convert ary
conv = []
for cb in CB:
    c, b = cb
    conv.extend([c] * b)

cl = len(conv)

sum = 0
for i in range(N):
    if i >= cl:
        sum += A[i]
    else:
        sum += max(A[i], conv[i])

print(sum)


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    CB.append((c, b))

# ascend
A.sort()

# descend
CB.sort(reverse=True)

# fill convert ary
conv = []
for cb in CB:
    c, b = cb
    conv.extend([c] * b)
    if len(conv) >= N:
        break

cl = len(conv)

sum = 0
for i in range(N):
    if i >= cl:
        sum += A[i]
    else:
        sum += max(A[i], conv[i])

print(sum)
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
from heapq import heapify, heappop, heappush
from sys import stdin

N, M = map(int, input().split())
A = list(map(int, stdin.readline().split()))
heapify(A)
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, stdin.readline().split()))
BC = sorted(BC, key=lambda x: x[1], reversed=True)

print(BC)

enough = False
for bc in BC:
    if enough:
        break
    for i in range(bc[0]):
        if bc[1] <= A[0]:
            enough = True
            break
        heappop(A)
        heappush(A, bc[1])

print(sum(A))


Fixed submission:
from heapq import heapify, heappop, heappush
from sys import stdin

N, M = map(int, input().split())
A = list(map(int, stdin.readline().split()))
heapify(A)
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, stdin.readline().split()))
BC = sorted(BC, key=lambda x: x[1], reverse=True)

enough = False
for bc in BC:
    if enough:
        break
    for i in range(bc[0]):
        if bc[1] <= A[0]:
            enough = True
            break
        heappop(A)
        heappush(A, bc[1])

print(sum(A))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, input().split()))
BC.sort(key=lambda tpl: tpl[1], reverse=True)  # sort by C descendant

D = [0] * N
i = 0
j = 0
while i < N and j < M:
    b, c = BC[j]
    for k in range(i, i + b):
        D[k] = c
    i += b
    j += 1

for i in range(N):
    A[i] = max(A[i], D[i])

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, input().split()))
BC.sort(key=lambda tpl: tpl[1], reverse=True)  # sort by C descendant

D = [0] * N
i = 0
j = 0
while i < N and j < M:
    b, c = BC[j]
    for k in range(i, min(i + b, N)):
        D[k] = c
    i += b
    j += 1

for i in range(N):
    A[i] = max(A[i], D[i])

print(sum(A))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
hand = []
for _ in range(m):
    b, c = map(int, input().split())
    hand.append((b, c))
hand.sort(key=lambda x: x[1], reverse=True)

i = 0
lena = len(a)
flag = False
for h in hand:
    for j in range(h[0]):
        if i < lena and a[i] >= h[1]:
            flag = True
            break
        else:
            a[i] = h[1]
            i += 1
    if flag:
        break

print(sum(a))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
hand = []
for _ in range(m):
    b, c = map(int, input().split())
    hand.append((b, c))
hand.sort(key=lambda x: x[1], reverse=True)

i = 0
lena = len(a)
flag = False
for h in hand:
    for j in range(h[0]):
        if i >= lena:
            flag = True
            break
        if a[i] >= h[1]:
            flag = True
            break
        else:
            a[i] = h[1]
            i += 1
    if flag:
        break

print(sum(a))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if i < N and A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()
print(sum(A))
Problem p03038 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long
int n, m;
vector<LL> a;
vector<pair<int, LL>> v;

int main() {
  cin >> n >> m;
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  sort(ALL(a));
  REP(i, m) {
    pair<int, LL> p;
    cin >> p.first >> p.second;
    v.push_back(p);
  }
  sort(ALL(v), [](pair<int, LL> p1, pair<int, LL> p2) {
    return p1.second > p2.second;
  });
  int ind = 0, ind_in;
  LL ret = 0;
  bool brk = false;
  REP(i, v.size()) {
    ind_in = ind;
    FOR(j, ind, ind + v[i].first) {
      if (a[j] < v[i].second) {
        ret += v[i].second;
        ind_in++;
      } else {
        brk = true;
        break;
      }
    }
    ind = ind_in;
    if (brk)
      break;
  }
  FOR(i, ind, n) { ret += a[i]; }

  cout << ret << endl;
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long
int n, m;
vector<LL> a;
vector<pair<int, LL>> v;

int main() {
  cin >> n >> m;
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  sort(ALL(a));
  REP(i, m) {
    pair<int, LL> p;
    cin >> p.first >> p.second;
    v.push_back(p);
  }
  sort(ALL(v), [](pair<int, LL> p1, pair<int, LL> p2) {
    return p1.second > p2.second;
  });
  int ind = 0, ind_in;
  LL ret = 0;
  bool brk = false;
  REP(i, v.size()) {
    ind_in = ind;
    FOR(j, ind, min(n, ind + v[i].first)) {
      if (a[j] < v[i].second) {
        ret += v[i].second;
        ind_in++;
      } else {
        brk = true;
        break;
      }
    }
    ind = ind_in;
    if (brk)
      break;
  }
  FOR(i, ind, n) { ret += a[i]; }

  cout << ret << endl;
  return 0;
}
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from heapq import heapify, heapreplace


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    As = list(map(int, input().split()))
    heapify(As)
    BCs = [None] * M
    for i in range(M):
        BCs[i] = list(map(int, input().split()))
    BCs = reversed(sorted(BCs, key=lambda x: x[1]))
    for B, C in BCs:
        for _ in range(B):
            if As[0] < C:
                heapreplace(As, C)
    print(sum(As))


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from heapq import heapify, heapreplace


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    As = list(map(int, input().split()))
    heapify(As)
    BCs = [None] * M
    for i in range(M):
        BCs[i] = list(map(int, input().split()))
    BCs = reversed(sorted(BCs, key=lambda x: x[1]))
    for B, C in BCs:
        for _ in range(B):
            if As[0] >= C:
                print(sum(As))
                sys.exit()
            heapreplace(As, C)
    print(sum(As))


if __name__ == "__main__":
    main()
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
import heapq
from sys import exit

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = sorted(
    [tuple(map(int, input().split())) for _ in range(M)],
    key=lambda x: x[1],
    reverse=True,
)
heapq.heapify(A)

ans = sum(A)
for b, c in BC:
    for _ in range(b):
        a = heapq.heappop(A)
        if a < c:
            ans += c - a
        else:
            print(ans)
            exit()
print(ans)


Fixed submission:
import heapq
from sys import exit

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = sorted(
    [tuple(map(int, input().split())) for _ in range(M)],
    key=lambda x: x[1],
    reverse=True,
)
heapq.heapify(A)

ans = sum(A)
for b, c in BC:
    for _ in range(b):
        if len(A) == 0:
            print(ans)
            exit()
        a = heapq.heappop(A)
        if a < c:
            ans += c - a
        else:
            print(ans)
            exit()
print(ans)
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
import heapq

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

# bc = sorted(bc, key=lambda x: x[1], reverse=True)
# print(bc)

heapq.heapify(a)
for b, c in bc:
    for i in range(b):
        num = heapq.heappop(a)
        if num < c:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, num)
            break
# print(a)
print(sum(a))


Fixed submission:
#!/usr/bin/env python3
import heapq

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

bc = sorted(bc, key=lambda x: x[1], reverse=True)
# print(bc)

heapq.heapify(a)
for b, c in bc:
    for i in range(b):
        num = heapq.heappop(a)
        if num < c:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, num)
            break
# print(a)
print(sum(a))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx > last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
        else:
            break
    else:
        idx += b
        print(a)
        print(idx)
        continue
    break

print(sum(a))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx > last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
    idx += b

print(sum(a))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict
import sys

input = sys.stdin.readline

N, M = [int(item) for item in input().split()]
A = [int(item) for item in input().split()]
A.sort()

new_dict = defaultdict(lambda: 0)
for i in range(M):
    b, c = [int(item) for item in input().split()]
    new_dict[c] += b
new_dict = sorted(new_dict.items(), key=lambda x: -x[0])

new_list = []
if len(new_dict) > 0:
    for k, v in new_dict:
        new_list += [k] * v
        if len(new_list) >= N:
            break

new_list = new_list[:N]

#####

n = min(N, len(new_list))
for i in range(n):
    if A[i] >= new_list[i]:
        break
    A[i] = new_list[i]

print(sum(A))


Fixed submission:
from collections import defaultdict
import sys

input = sys.stdin.readline

N, M = [int(item) for item in input().split()]
A = [int(item) for item in input().split()]
A.sort()

new_dict = defaultdict(lambda: 0)
for i in range(M):
    b, c = [int(item) for item in input().split()]
    new_dict[c] += b
new_dict = sorted(new_dict.items(), key=lambda x: -x[0])

new_list = []
for k, v in new_dict:
    if v > N:
        v = N
    new_list += [k] * v
    if len(new_list) >= N:
        break

new_list = new_list[:N]

#####

n = min(N, len(new_list))
for i in range(n):
    if A[i] >= new_list[i]:
        break
    A[i] = new_list[i]

print(sum(A))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
As = list(map(int, input().split()))
BCs = [tuple(map(int, input().split())) for _ in range(M)]

As.sort()
BCs.sort(key=lambda x: x[1], reverse=True)

iA = 0
for B, C in BCs:
    for j in range(B):
        if As[iA] < C:
            As[iA] = C
            iA += 1
        else:
            break
    else:
        continue
    break

print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
BCs = [tuple(map(int, input().split())) for _ in range(M)]

As.sort()
BCs.sort(key=lambda x: x[1], reverse=True)

iA = 0
for B, C in BCs:
    for j in range(B):
        if As[iA] < C:
            As[iA] = C
            iA += 1
            if iA >= N:
                break
        else:
            break
    else:
        continue
    break

print(sum(As))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
A = list(map(int, input().split()))

CB = []
for _ in range(m):
    b, c = map(int, input().split())
    CB.append([c, b])

CB.sort(reverse=True)
A.sort()
index = 0
for i in range(m):
    if index >= n:
        break
    elif A[index] >= CB[i][0]:
        break

    for j in range(CB[i][1]):
        if A[index] >= CB[i][0]:
            break

        A[index] = CB[i][0]
        index += 1

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

CB = []
for _ in range(m):
    b, c = map(int, input().split())
    CB.append([c, b])

CB.sort(reverse=True)
A.sort()
index = 0
for i in range(m):
    if index >= n:
        break
    elif A[index] >= CB[i][0]:
        break

    for j in range(CB[i][1]):
        if index >= n:
            break
        elif A[index] >= CB[i][0]:
            break

        A[index] = CB[i][0]
        index += 1

print(sum(A))
Problem p03038 (C++)

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  vector<pair<int, int>> op(m);
  for (int i = 0; i < m; i++) {
    cin >> op[i].first >> op[i].second;
  }
  sort(op.begin(), op.end(),
       [&](auto x, auto y) { return x.second > y.second; });
  sort(a.rbegin(), a.rend());
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    while (op[i].first--) {
      if (a.back() < op[i].second) {
        sum += 1LL * op[i].second;
        a.pop_back();
      } else
        break;
    }
  }
  for (int x : a)
    sum += 1LL * x;
  cout << sum << '\n';
}

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  vector<pair<int, int>> op(m);
  for (int i = 0; i < m; i++) {
    cin >> op[i].first >> op[i].second;
  }
  sort(op.begin(), op.end(),
       [&](auto x, auto y) { return x.second > y.second; });
  sort(a.rbegin(), a.rend());
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    while (op[i].first--) {
      if (!a.empty() and a.back() < op[i].second) {
        sum += 1LL * op[i].second;
        a.pop_back();
      } else
        break;
    }
  }
  for (int x : a)
    sum += 1LL * x;
  cout << sum << '\n';
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  int n, m, i, j;
  ll answer;
  int tmpa, tmpb, tmpc;
  vector<int> d;
  int endmark;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> ope(n);
  bool end = false;

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

  for (i = 0; i < m; i++)
    cin >> ope[i].second >> ope[i].first;
  sort(ope.begin(), ope.end());

  endmark = 0;
  for (i = ope.size() - 1; i >= 0; i--) {
    int c = ope[i].first;
    int b = ope[i].second;
    for (j = 0; j < b; j++) {
      d.push_back(c);
      endmark++;
      if (endmark == n) {
        end = true;
        break;
      }
    }
    if (end)
      break;
  }

  for (i = 0; i < n; i++) {
    if (a[i] >= d[i])
      break;
    else
      a[i] = d[i];
  }

  answer = 0;
  for (i = 0; i < a.size(); i++)
    answer += a[i];

  cout << answer << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  int n, m, i, j;
  ll answer;
  int tmpa, tmpb, tmpc;
  vector<int> d;
  int endmark;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> ope(m);
  bool end = false;

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

  for (i = 0; i < m; i++)
    cin >> ope[i].second >> ope[i].first;
  sort(ope.begin(), ope.end());

  endmark = 0;
  for (i = ope.size() - 1; i >= 0; i--) {
    int c = ope[i].first;
    int b = ope[i].second;
    for (j = 0; j < b; j++) {
      d.push_back(c);
      endmark++;
      if (endmark == n) {
        end = true;
        break;
      }
    }
    if (end)
      break;
  }

  for (i = 0; i < n; i++) {
    if (a[i] >= d[i])
      break;
    else
      a[i] = d[i];
  }

  answer = 0;
  for (i = 0; i < a.size(); i++)
    answer += a[i];

  cout << answer << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
// Created by martinezdiego on 27/5/19

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int a[n];
  pair<int, int> bc[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> bc[i].first >> bc[i].second;
  }
  sort(bc, bc + m,
       [](pair<int, int> a, pair<int, int> b) { return a.second > b.second; });
  bool finished = false;
  for (int i = 0, p = 0; i < m and !finished; i++) {
    while (bc[i].first and !finished) {
      if (a[p] < bc[i].second)
        a[p] = bc[i].second;
      else
        finished = true;
      p++;
      bc[i].first--;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
// Created by martinezdiego on 27/5/19

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int a[n];
  pair<int, int> bc[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> bc[i].first >> bc[i].second;
  }
  sort(bc, bc + m,
       [](pair<int, int> a, pair<int, int> b) { return a.second > b.second; });
  bool finished = false;
  for (int i = 0, p = 0; i < m and p < n and !finished; i++) {
    while (bc[i].first and p < n and !finished) {
      if (a[p] < bc[i].second)
        a[p] = bc[i].second;
      else
        finished = true;
      p++;
      bc[i].first--;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << '\n';
  return 0;
}
Problem p03038 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N, M;
double dd, cc;
ll A[101010];
ll i, j, k, l;
vector<pll> cbv;
ll ans;

int main() {
  scanf("%lld%lld", &N, &M);
  fornum(i, 0, N) { scanf("%lld", &A[i]); }
  sort(A, A + N);
  fornum(i, 0, M) {
    ll b, c;
    scanf("%lld%lld", &b, &c);
    cbv.push_back({c, b});
  }
  sort(cbv.begin(), cbv.end(), greater<pll>());
  j = 0;
  fornum(i, 0, M) {
    ll b = cbv[i].second;
    ll c = cbv[i].first;
    while (b > 0 && A[j] < c) {
      A[j++] = c;
      b--;
    }
  }
  fornum(i, 0, N) { ans += A[i]; }
  printf("%lld", ans);
  return 0;
}

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N, M;
double dd, cc;
ll A[101010];
ll i, j, k, l;
vector<pll> cbv;
ll ans;

int main() {
  scanf("%lld%lld", &N, &M);
  fornum(i, 0, N) { scanf("%lld", &A[i]); }
  sort(A, A + N);
  fornum(i, 0, M) {
    ll b, c;
    scanf("%lld%lld", &b, &c);
    cbv.push_back({c, b});
  }
  sort(cbv.begin(), cbv.end(), greater<pll>());
  j = 0;
  fornum(i, 0, M) {
    ll b = cbv[i].second;
    ll c = cbv[i].first;
    while (b > 0 && j < N && A[j] < c) {
      A[j++] = c;
      b--;
    }
  }
  fornum(i, 0, N) { ans += A[i]; }
  printf("%lld", ans);
  return 0;
}
Problem p03038 (C++)

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

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  long long int b, c;
  vector<long long int> A(N);
  vector<pair<long long int, long long int>> BC;
  int i, j = 0, k;
  for (i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  for (j = 0; j < M; j++) {
    scanf("%lld %lld", &b, &c);
    BC.push_back(make_pair(c, b));
  }

  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end(), greater<pair<long long int, long long int>>());
  int f = 0;
  int pi = 0;
  j = 0;
  while (j < M && f == 0) {
    k = BC[j].second - 1 + pi;
    while (A[k] > BC[j].first) {
      if (k != 0)
        k--;
      else {
        f = 1;
        break;
      }
    }
    if (f == 1)
      break;
    for (int l = pi; l <= k; l++) {
      A[l] = BC[j].first;
    }
    pi = k + 1;
    j++;
  }

  long long int res = 0;
  for (i = 0; i < N; i++) {
    res += A[i];
  }
  printf("%lld", res);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  long long int b, c;
  vector<long long int> A(N);
  vector<pair<long long int, long long int>> BC;
  int i, j = 0, k;
  for (i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  for (j = 0; j < M; j++) {
    scanf("%lld %lld", &b, &c);
    BC.push_back(make_pair(c, b));
  }

  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end(), greater<pair<long long int, long long int>>());
  int f = 0;
  int pi = 0;
  j = 0;
  while (j < M && f == 0) {
    k = BC[j].second - 1 + pi;
    if (k > N)
      k = N - 1;
    while (A[k] > BC[j].first) {
      if (k != 0)
        k--;
      else {
        f = 1;
        break;
      }
    }
    if (f == 1)
      break;
    for (int l = pi; l <= k; l++) {
      A[l] = BC[j].first;
    }
    pi = k + 1;
    j++;
  }

  long long int res = 0;
  for (i = 0; i < N; i++) {
    res += A[i];
  }
  printf("%lld", res);

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
// #define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

int ceil(int x, int y) { return (x % y == 0) ? x / y : x / y + 1; }

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }

int lcm(int x, int y) { return x / gcd(x, y) * y; }

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

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

  vector<pii> cb(m);
  for (int i = 0; i < m; i++) {
    cb[i] = make_pair(c[i], b[i]);
  }
  sort(cb.begin(), cb.end());

  priority_queue<int, vector<int>, greater<int>> pq;
  for (int i = 0; i < n; i++) {
    pq.push(a[i]);
  }

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < cb[i].second && pq.top() < cb[i].first; j++) {
      pq.pop();
      pq.push(cb[i].first);
    }
  }

  ll sum = 0;
  while (!pq.empty()) {
    sum += pq.top();
    pq.pop();
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
// #define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

int ceil(int x, int y) { return (x % y == 0) ? x / y : x / y + 1; }

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }

int lcm(int x, int y) { return x / gcd(x, y) * y; }

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

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

  vector<pii> cb(m);
  for (int i = 0; i < m; i++) {
    cb[i] = make_pair(c[i], b[i]);
  }
  sort(cb.begin(), cb.end());

  priority_queue<int, vector<int>, greater<int>> pq;
  for (int i = 0; i < n; i++) {
    pq.push(a[i]);
  }

  for (int i = m - 1; i >= 0; i--) {
    for (int j = 0; j < cb[i].second && pq.top() < cb[i].first; j++) {
      pq.pop();
      pq.push(cb[i].first);
    }
  }

  ll sum = 0;
  while (!pq.empty()) {
    sum += pq.top();
    pq.pop();
  }

  cout << sum << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<pair<int, int>> CB(N);
  for (int i = 0; i < M; i++)
    cin >> CB[i].second >> CB[i].first;
  sort(CB.begin(), CB.end(), greater<pair<int, int>>());
  int t = 0, i = 0;
  while (t <= N && i < M) {
    for (int _ = 0; _ < CB[i].second; _++)
      A.push_back(CB[i].first);
    t += CB[i].second;
    i++;
  }
  sort(A.begin(), A.end(), greater<int>());
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<pair<int, int>> CB(M);
  for (int i = 0; i < M; i++)
    cin >> CB[i].second >> CB[i].first;
  sort(CB.begin(), CB.end(), greater<pair<int, int>>());
  int t = 0, i = 0;
  while (t <= N && i < M) {
    for (int _ = 0; _ < CB[i].second; _++)
      A.push_back(CB[i].first);
    t += CB[i].second;
    i++;
  }
  sort(A.begin(), A.end(), greater<int>());
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i];
  cout << ans;
}