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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M, ans = 0;
  cin >> N >> M;
  vector<long long> A(N);
  vector<long long> B(N);
  map<long long, long long> mp;
  long long visit = -1;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A.at(i) = a;
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < M; i++) {
    long long B, C;
    cin >> B >> C;
    mp[C] += B;
  }
  int check = 0, rock = 0;
  for (auto i = mp.end(); i != mp.begin(); --i) {
    if (check == 0) {
      check++;
      continue;
    }
    long long x = i->first;
    long long y = i->second;
    for (int j = visit + 1; j < visit + 1 + y; j++) {
      B.at(j) = x;
      if (j == N - 1) {
        rock = 54;
        break;
      }
    }
    if (rock == 54) {
      for (int i = 0; i < N; i++) {
        ans += max(B.at(i), A.at(i));
      }
      cout << ans << endl;
      return 0;
    }
    visit = max(visit, visit + y);
  }
  long long X = mp.begin()->first;
  long long Y = mp.begin()->second;
  for (int i = visit + 1; i < visit + 1 + Y; i++) {
    B.at(i) = X;
  }
  for (int i = 0; i < N; i++) {
    ans += max(B.at(i), A.at(i));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M, ans = 0;
  cin >> N >> M;
  vector<long long> A(N);
  vector<long long> B(N);
  map<long long, long long> mp;
  long long visit = -1;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A.at(i) = a;
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < M; i++) {
    long long B, C;
    cin >> B >> C;
    mp[C] += B;
  }
  int check = 0, rock = 0;
  for (auto i = mp.end(); i != mp.begin(); --i) {
    if (check == 0) {
      check++;
      continue;
    }
    long long x = i->first;
    long long y = i->second;
    for (int j = visit + 1; j < visit + 1 + y; j++) {
      B.at(j) = x;
      if (j == N - 1) {
        rock = 54;
        break;
      }
    }
    if (rock == 54) {
      for (int i = 0; i < N; i++) {
        ans += max(B.at(i), A.at(i));
      }
      cout << ans << endl;
      return 0;
    }
    visit = max(visit, visit + y);
  }
  long long X = mp.begin()->first;
  long long Y = mp.begin()->second;
  for (int i = visit + 1; i < visit + 1 + Y; i++) {
    B.at(i) = X;
    if (i == N - 1) {
      rock = 54;
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    ans += max(B.at(i), A.at(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <vector>

using namespace std;

// 定义比较结构
struct cmp1 {
  bool operator()(int &a, int &b) {
    return a > b; // 最小值优先
  }
};

std::priority_queue<int, vector<int>, cmp1> q; // 最小值优先

int n, m;
int a[100010];

struct Card {
  int value;
  int count;
};
Card cards[100010];

int r[100010];

bool compareCard(const Card &a, const Card &b) { return a.value > b.value; }

bool compare(const int &a, const int &b) { return a < b; }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &(a[i]));

  for (int i = 0; i < m; i++)
    scanf("%d%d", &(cards[i].count), &(cards[i].value));

  sort(cards, cards + m, compareCard);

  int szR = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < cards[i].count; j++) {
      r[szR] = cards[i].value;
      szR++;
      if (szR > n)
        break;
      //			q.push(cards[i].value);
      //
      //			if (q.size() >= n)
      //				break;
    }
  }

  //	int limit = q.size();
  //
  //	for (int i = limit - 1 ; i >= 0 ; i--)
  //	{
  //		r[i] = q.top();
  //		q.pop();
  //	}

  sort(a, a + n, compare);

  //	for (int i = 0 ; i < limit ; i++)
  for (int i = 0; i < szR; i++) {
    if (r[i] > a[i])
      a[i] = r[i];
  }

  long long sum = 0;
  for (int i = 0; i < n; i++)
    sum = sum + a[i];

  printf("%lld\n", sum);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <vector>

using namespace std;

// 定义比较结构
struct cmp1 {
  bool operator()(int &a, int &b) {
    return a > b; // 最小值优先
  }
};

std::priority_queue<int, vector<int>, cmp1> q; // 最小值优先

int n, m;
int a[100010];

struct Card {
  int value;
  int count;
};
Card cards[100010];

int r[100010];

bool compareCard(const Card &a, const Card &b) { return a.value > b.value; }

bool compare(const int &a, const int &b) { return a < b; }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &(a[i]));

  for (int i = 0; i < m; i++)
    scanf("%d%d", &(cards[i].count), &(cards[i].value));

  sort(cards, cards + m, compareCard);

  int szR = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < cards[i].count; j++) {
      if (szR > n)
        break;

      r[szR] = cards[i].value;
      szR++;
      if (szR > n)
        break;
      //			q.push(cards[i].value);
      //
      //			if (q.size() >= n)
      //				break;
    }
  }

  //	int limit = q.size();
  //
  //	for (int i = limit - 1 ; i >= 0 ; i--)
  //	{
  //		r[i] = q.top();
  //		q.pop();
  //	}

  sort(a, a + n, compare);

  //	for (int i = 0 ; i < limit ; i++)
  for (int i = 0; i < szR; i++) {
    if (r[i] > a[i])
      a[i] = r[i];
  }

  long long sum = 0;
  for (int i = 0; i < n; i++)
    sum = sum + a[i];

  printf("%lld\n", sum);

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  vp v;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v.pb({c, b});
  }
  sort(all(a));
  sort(all(v));
  reverse(all(v));
  int j = 0;
  rep(i, n) {
    if (a[i] < v[j].fi) {
      a[i] = v[j].fi;
      v[j].se--;
    }
    if (v[j].se == 0)
      j++;
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  vp v;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v.pb({c, b});
  }
  sort(all(a));
  sort(all(v));
  reverse(all(v));
  int j = 0;
  rep(i, n) {
    if (j == v.size())
      break;
    if (a[i] < v[j].fi) {
      a[i] = v[j].fi;
      v[j].se--;
    }
    if (v[j].se == 0)
      j++;
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}
Problem p03038 (Python)

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

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

change = []
for m in range(M):
    B, C = map(int, input().split())
    change.append((B, C))
change.sort(key=lambda x: -x[1])

change2 = []
for c in change:
    change2.extend([c[1]] * c[0])
change2 = change2[:N]


heapq.heapify(A)

for c in change2:
    x = heapq.heappop(A)
    if x >= c:
        # 先頭すでに十分大きい場合は抜ける
        heapq.heappush(A, x)
        break
    else:
        x = max(x, c)
        heapq.heappush(A, x)

print(sum(A))


Fixed submission:
import heapq

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

change = []
for m in range(M):
    B, C = map(int, input().split())
    change.append((B, C))
change.sort(key=lambda x: -x[1])

change2 = []
for c in change:
    change2.extend([c[1]] * c[0])
    if len(change2) >= N:
        change2 = change2[:N]
        break


heapq.heapify(A)

for c in change2:
    x = heapq.heappop(A)
    if x >= c:
        # 先頭すでに十分大きい場合は抜ける
        heapq.heappush(A, x)
        break
    else:
        x = max(x, c)
        heapq.heappush(A, x)

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

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  map<int, int> mp;
  priority_queue<l_l> q;
  rep(i, n) {
    cin >> a[i];
    q.push(l_l(a[i], 1));
  }
  vector<int> b(m);
  vector<int> c(m);
  rep(i, m) cin >> b[i] >> c[i];
  rep(i, m) { q.push(l_l(c[i], b[i])); }
  ll ans = 0;
  int cnt = 0;
  while (cnt + q.top().second <= n) {
    ans += q.top().first * q.top().second;
    cnt += q.top().second;
    q.pop();
  }
  ans += q.top().first * (n - cnt);
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  map<int, int> mp;
  priority_queue<l_l> q;
  rep(i, n) {
    cin >> a[i];
    q.push(l_l(a[i], 1));
  }
  vector<int> b(m);
  vector<int> c(m);
  rep(i, m) cin >> b[i] >> c[i];
  rep(i, m) { q.push(l_l(c[i], b[i])); }
  ll ans = 0;
  int cnt = 0;
  while (cnt + q.top().second <= n) {
    ans += q.top().first * q.top().second;
    cnt += q.top().second;
    q.pop();
  }
  ans += q.top().first * (n - cnt);
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<int, vector<int>, greater<int>> q;
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a);
  }
  vector<vector<ll>> cng(m, vector<ll>(2));
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    cng[i][0] = c;
    cng[i][1] = b;
  }
  sort(cng.begin(), cng.end());
  rep(i, m) {
    ll cnt = 0;
    while (q.top() < cng[i][0]) {
      q.pop();
      q.push(cng[i][0]);
      cnt++;
      if (cnt == cng[i][1])
        break;
    }
  }
  ll ans = 0;
  rep(i, n) {
    ans += q.top();
    q.pop();
  }
  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<int, vector<int>, greater<int>> q;
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a);
  }
  vector<vector<ll>> cng(m, vector<ll>(2));
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    cng[i][0] = c;
    cng[i][1] = b;
  }
  sort(cng.begin(), cng.end());
  reverse(cng.begin(), cng.end());
  rep(i, m) {
    ll cnt = 0;
    while (q.top() < cng[i][0]) {
      q.pop();
      q.push(cng[i][0]);
      cnt++;
      if (cnt == cng[i][1])
        break;
    }
  }
  ll ans = 0;
  rep(i, n) {
    ans += q.top();
    q.pop();
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  sort(a.begin(), a.end());
  vector<int> b(m), c(m);
  for (int j = 0; j < m; j++) {
    cin >> b.at(j) >> c.at(j);
  }
  int l = accumulate(b.begin(), b.end(), 0);
  vector<pair<int, int>> pairs(m);
  for (int j = 0; j < m; j++) {
    pairs[j] = make_pair(c.at(j), b.at(j));
  }
  // firstが小さい順、secondが小さい順にソート
  sort(pairs.begin(), pairs.end());
  reverse(pairs.begin(), pairs.end());
  //  for(int j = 0; j < m; j++) {
  //    cout << pairs[j].first << endl;
  //  }
  int i = 0;
  int flag = 0;
  for (int j = 0; j < m; j++) {
    for (int k = 0; k < pairs[j].second; k++) {
      if (a.at(i) > pairs[j].first) {
        flag = 1;
        ++i;
        break;
      } else {
        a.at(i) = pairs[j].first;
        ++i;
      }
    }
    if (flag == 1) {
      break;
    }
  }
  long long s = accumulate(a.begin(), a.end(), 0LL);
  cout << s << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  sort(a.begin(), a.end());
  vector<int> b(m), c(m);
  for (int j = 0; j < m; j++) {
    cin >> b.at(j) >> c.at(j);
  }
  int l = accumulate(b.begin(), b.end(), 0);
  vector<pair<int, int>> pairs(m);
  for (int j = 0; j < m; j++) {
    pairs[j] = make_pair(c.at(j), b.at(j));
  }
  // firstが小さい順、secondが小さい順にソート
  sort(pairs.begin(), pairs.end());
  reverse(pairs.begin(), pairs.end());
  //  for(int j = 0; j < m; j++) {
  //    cout << pairs[j].first << endl;
  //  }
  int i = 0;
  int flag = 0;
  for (int j = 0; j < m; j++) {
    for (int k = 0; k < pairs[j].second; k++) {
      if (i == n) {
        flag = 1;
        break;
      }
      if (a.at(i) > pairs[j].first) {
        flag = 1;
        ++i;
        break;
      } else {
        a.at(i) = pairs[j].first;
        ++i;
      }
    }
    if (flag == 1) {
      break;
    }
  }
  long long s = accumulate(a.begin(), a.end(), 0LL);
  cout << s << endl;
}
Problem p03038 (C++)

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

int main() {
  long long n, m, i, j, sum;
  cin >> n >> m;
  priority_queue<long long, vector<long long>, greater<long long>> a;
  vector<long long> b(m), c(m);
  vector<pair<long long, long long>> bc(m);
  for (i = 0; i < n; ++i) {
    long long z;
    cin >> z;
    a.push(z);
  }
  for (i = 0; i < m; ++i) {
    cin >> b[i] >> c[i];
    bc[i] = make_pair(c[i], b[i]);
  }
  sort(bc.begin(), bc.end());

  for (i = 0; i < m; ++i) {
    j = 0;
    long long hi;
    hi = a.top();
    while (j < bc[i].second && hi < bc[i].first) {
      a.pop();
      a.push(bc[i].first);
      hi = a.top();
      ++j;
    }
  }
  sum = 0;
  while (!a.empty()) {
    sum += a.top();
    a.pop();
  }
  cout << sum << endl;
  return 0;
}


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

int main() {
  long long n, m, i, j, sum;
  cin >> n >> m;
  priority_queue<long long, vector<long long>, greater<long long>> a;
  vector<long long> b(m), c(m);
  vector<pair<long long, long long>> bc(m);
  for (i = 0; i < n; ++i) {
    long long z;
    cin >> z;
    a.push(z);
  }
  for (i = 0; i < m; ++i) {
    cin >> b[i] >> c[i];
    bc[i] = make_pair(c[i], b[i]);
  }
  sort(bc.begin(), bc.end(), greater<>());

  for (i = 0; i < m; ++i) {
    j = 0;
    long long hi;
    hi = a.top();
    while (j < bc[i].second && hi < bc[i].first) {
      a.pop();
      a.push(bc[i].first);
      hi = a.top();
      ++j;
    }
  }
  sum = 0;
  while (!a.empty()) {
    sum += a.top();
    a.pop();
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;

#define sz(a) int(a.size())

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int &t : a)
    cin >> t;

  vector<pair<int, int>> cb(m);
  for (auto &t : cb) {
    int &c = t.first, &b = t.second;
    cin >> b >> c;
  }
  sort(rbegin(cb), rend(cb));

  vector<int> cur;
  for (auto t : cb) {
    int c = t.first, b = t.second;
    while (sz(cur) < n && b--)
      cur.push_back(c);
  }
  sort(begin(a), end(a));

  long long ans = 0;
  for (int i = 0; i < n; i++)
    ans += max(cur[i], a[i]);
  cout << ans << endl;
}


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

using namespace std;

#define sz(a) int(a.size())

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int &t : a)
    cin >> t;

  vector<pair<int, int>> cb(m);
  for (auto &t : cb) {
    int &c = t.first, &b = t.second;
    cin >> b >> c;
  }
  sort(rbegin(cb), rend(cb));

  vector<int> cur;
  for (auto t : cb) {
    int c = t.first, b = t.second;
    while (sz(cur) < n && b--)
      cur.push_back(c);
  }
  while (sz(cur) < n)
    cur.push_back(0);
  sort(begin(a), end(a));

  long long ans = 0;
  for (int i = 0; i < n; i++)
    ans += max(cur[i], a[i]);
  cout << ans << endl;
}
Problem p03038 (C++)

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

const ll infll = 9223372036854775806;
const int inf = 2147483646;

int main() {
  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll i, j, k, n, m, a[100000], t, ans = 0, b1, c1;
  cin >> n >> m;
  vector<pair<int, int>> b;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < m; i++) {
    cin >> b1 >> c1;
    b.push_back(make_pair(c1, b1));
  }
  sort(a, a + n);
  sort(b.begin(), b.end());
  t = m - 1;
  for (i = 0; i < n; i++) {
    if (b[t].second == 0) {
      t--;
    }
    if (t >= 0 && b[t].first > a[i]) {
      ans += b[t].first;
      b[t].second--;
    } else {
      ans += a[i];
    }
  }
  cout << ans;
  return 0;
}

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

const ll infll = 9223372036854775806;
const int inf = 2147483646;

int main() {
  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll i, j, k, n, m, a[100000], t, ans = 0, b1, c1;
  cin >> n >> m;
  vector<pair<int, int>> b;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < m; i++) {
    cin >> b1 >> c1;
    b.push_back(make_pair(c1, b1));
  }
  sort(a, a + n);
  sort(b.begin(), b.end());
  t = m - 1;
  for (i = 0; i < n; i++) {
    if (t >= 0 && b[t].second == 0) {
      t--;
    }
    if (t >= 0 && b[t].first > a[i]) {
      ans += b[t].first;
      b[t].second--;
    } else {
      ans += a[i];
    }
  }
  cout << ans;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

bool my_compare(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.first != b.first) {
    return a.first > b.first;
  }
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return true;
  }
}
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<pair<ll, ll>> CB(M);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++) {
    cin >> CB[i].second >> CB[i].first;
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end(), my_compare);
  ll cur_min = A[0], cur_min_idx = 0, replace_size, replace_num;
  for (int i = 0; i < M; i++) {
    replace_size = CB[i].second;
    replace_num = CB[i].first;
    if (cur_min < replace_num) {
      ll upper =
          (cur_min_idx + replace_size < N) ? cur_min_idx + replace_size : N;
      for (int j = cur_min_idx; j < upper && A[j] < replace_num; j++) {
        A[j] = replace_num;
      }
      if (upper == N)
        break;
      cur_min_idx = upper;
      cur_min = A[cur_min_idx];
    } else {
      break;
    }
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

bool my_compare(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.first != b.first) {
    return a.first > b.first;
  }
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return false;
  }
}
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  vector<pair<ll, ll>> CB(M);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++) {
    cin >> CB[i].second >> CB[i].first;
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end(), my_compare);
  ll cur_min = A[0], cur_min_idx = 0, replace_size, replace_num;
  for (int i = 0; i < M; i++) {
    replace_size = CB[i].second;
    replace_num = CB[i].first;
    if (cur_min < replace_num) {
      ll upper =
          (cur_min_idx + replace_size < N) ? cur_min_idx + replace_size : N;
      for (int j = cur_min_idx; j < upper && A[j] < replace_num; j++) {
        A[j] = replace_num;
      }
      if (upper == N)
        break;
      cur_min_idx = upper;
      cur_min = A[cur_min_idx];
    } else {
      break;
    }
  }
  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 <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B, std::vector<long long> C) {
  vector<pair<ll, ll>> BC;
  for (ll i = 0; i < M; ++i) {
    BC.emplace_back(B[i], C[i]);
  }
  sort(begin(BC), end(BC),
       [](auto &&a, auto &&b) { return a.second > b.second; });

  sort(begin(A), end(A));
  ll w_idx = 0;
  for (auto &&bc : BC) {
    for (ll i = 0; i < bc.first && w_idx < (ll)A.size(); ++i) {
      if (A[w_idx] < bc.second) {
        A[w_idx] = bc.second;
        w_idx++;
      }
    }
  }

  ll ans = accumulate(begin(A), end(A), 0LL);
  cout << ans << "\n";
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    cin.tie(nullptr);
    cout.tie(nullptr);
	ios_base::sync_with_stdio(false);
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    std::vector<long long> B(M);
    std::vector<long long> C(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&B[i]);
        scanf("%lld",&C[i]);
    }
    solve(N, M, std::move(A), std::move(B), std::move(C));
    return 0;
}


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

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B, std::vector<long long> C) {
  vector<pair<ll, ll>> BC;
  for (ll i = 0; i < M; ++i) {
    BC.emplace_back(B[i], C[i]);
  }
  sort(begin(BC), end(BC),
       [](auto &&a, auto &&b) { return a.second > b.second; });

  sort(begin(A), end(A));
  ll w_idx = 0;
  for (auto &&bc : BC) {
    for (ll i = 0; i < bc.first && w_idx < (ll)A.size() && A[w_idx] < bc.second;
         ++i) {
      A[w_idx] = bc.second;
      w_idx++;
    }
  }

  ll ans = accumulate(begin(A), end(A), 0LL);
  cout << ans << "\n";
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    cin.tie(nullptr);
    cout.tie(nullptr);
	ios_base::sync_with_stdio(false);
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    std::vector<long long> B(M);
    std::vector<long long> C(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&B[i]);
        scanf("%lld",&C[i]);
    }
    solve(N, M, std::move(A), std::move(B), std::move(C));
    return 0;
}
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
"""
解説を参考に作成 その3
"""


def solve():
    N, M = map(int, input().split())
    A = [int(i) for i in input().split()]
    A.sort()
    CB = [list(reversed([int(i) for i in input().split()])) for _ in range(M)]
    CB = list(sorted(CB, reverse=True))

    count = 0
    flag = False
    for cb in CB:
        for b in range(cb[1]):
            if A[count] < cb[0]:
                A[count] = cb[0]
            else:
                flag = True
                break
            count += 1
        if flag:
            break
    print(sum(A))


if __name__ == "__main__":
    solve()


Fixed submission:
"""
解説を参考に作成 その3
"""


def solve():
    N, M = map(int, input().split())
    A = [int(i) for i in input().split()]
    A.sort()
    CB = [list(reversed([int(i) for i in input().split()])) for _ in range(M)]
    CB = list(sorted(CB, reverse=True))

    count = 0
    flag = False
    for cb in CB:
        for b in range(cb[1]):
            if A[count] < cb[0]:
                A[count] = cb[0]
            else:
                flag = True
                break
            count += 1
            if count >= N:
                flag = True
                break

        if flag:
            break
    print(sum(A))


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

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
bc_list = [list(map(int, input().split())) for _ in range(m)]
a_list.sort()
bc_list.sort(key=lambda i: -i[1])

a_idx = 0
bc_idx = 0
while bc_idx < len(bc_list):
    b, c = bc_list[bc_idx]
    finish = False
    for _ in range(b):
        if c > a_list[a_idx]:
            a_list[a_idx] = c
            a_idx += 1
        else:
            finish = True
            break
    if finish:
        break
    bc_idx += 1

print(sum(a_list))


Fixed submission:
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
bc_list = [list(map(int, input().split())) for _ in range(m)]
a_list.sort()
bc_list.sort(key=lambda i: -i[1])

a_idx = 0
bc_idx = 0
while bc_idx < len(bc_list):
    b, c = bc_list[bc_idx]
    finish = False
    for _ in range(b):
        if a_idx >= len(a_list):
            finish = True
            break
        if c > a_list[a_idx]:
            a_list[a_idx] = c
            a_idx += 1
        else:
            finish = True
            break
    if finish:
        break
    bc_idx += 1

print(sum(a_list))
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()
for i in range(M):
    B, C = [int(i) for i in input().strip().split()]
    is_changed = False
    for j in range(0, B):
        if A[j] < C:
            A[j] = C
            is_changed = True
        else:
            break
    if is_changed:
        A.sort()
print(sum(A))


Fixed submission:
N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()

BC = [[0, 0] for _ in range(M)]
for j in range(M):
    BC[j][0], BC[j][1] = [int(i) for i in input().strip().split()]
BC.sort(key=lambda x: x[1])

res = 0
cnt = 0
i = N - 1
j = M - 1
while True:
    if cnt == N:
        break

    if j == -1:
        res += A[i]
        i -= 1
    elif A[i] > BC[j][1]:
        res += A[i]
        i -= 1
    elif A[i] <= BC[j][1]:
        res += BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j -= 1
    else:
        raise ValueError
    cnt += 1
print(res)
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().rstrip().split())
A = list(map(int, input().rstrip().split()))

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

Index = sorted(range(M), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    if A[0] >= C[index]:
        break
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)


Fixed submission:
#!/usr/bin/env python3
N, M = map(int, input().rstrip().split())
A = list(map(int, input().rstrip().split()))

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

Index = sorted(range(M), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    if A and A[0] >= C[index]:
        break
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)
Problem p03038 (Python)

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

N, M = map(int, input().split())
A_LIST = list(map(int, input().split()))
BC_LIST = []
heapq.heapify(A_LIST)
for i in range(M):
    b, c = map(int, input().split())
    BC_LIST.append([b, c])
BC_LIST.sort(reverse=True, key=lambda x: x[1])  # C降順にソート
for b, c in BC_LIST:
    for _ in range(b):
        tmp = heapq.heappop(A_LIST)
        heapq.heappush(A_LIST, max(tmp, c))
print(sum(A_LIST))


Fixed submission:
import heapq

N, M = map(int, input().split())
A_LIST = list(map(int, input().split()))
BC_LIST = []
heapq.heapify(A_LIST)
for i in range(M):
    b, c = map(int, input().split())
    BC_LIST.append([b, c])
BC_LIST.sort(reverse=True, key=lambda x: x[1])  # C降順にソート
for b, c in BC_LIST:
    for _ in range(b):
        tmp = heapq.heappop(A_LIST)
        if tmp < c:
            heapq.heappush(A_LIST, c)
        else:
            heapq.heappush(A_LIST, tmp)
            break
print(sum(A_LIST))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [0] * m
ans = []
a.sort()
for i in range(m):
    bc[i] = list(map(int, input().split()))
bc.sort(key=lambda x: x[1], reverse=True)


j = 0
for i in range(n):
    if bc[j][0] == 0:
        j += 1
    if j >= m:
        ans.append(a[i])
    elif a[i] >= bc[j][1]:
        ans.append(a[i])
    else:
        ans.append(bc[j][1])
        bc[j][0] -= 1

print(sum(ans))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [0] * m
ans = []
a.sort()
for i in range(m):
    bc[i] = list(map(int, input().split()))
bc.sort(key=lambda x: x[1], reverse=True)


j = 0
for i in range(n):
    if j < m:
        if bc[j][0] == 0:
            j += 1
    if j >= m:
        ans.append(a[i])
    elif a[i] >= bc[j][1]:
        ans.append(a[i])
    else:
        ans.append(bc[j][1])
        bc[j][0] -= 1

print(sum(ans))
Problem p03038 (Python)

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

input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(M)]

cards = sorted(A)
op.sort(key=lambda x: -x[1])

i = 0

for m in range(M):
    b, c = op[m]
    for _ in range(b):
        if cards[i] < c:
            cards[i] = c
            i += 1
        if i >= N:
            break
    else:
        continue
    break

print(sum(cards))


Fixed submission:
import sys

input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(M)]

cards = sorted(A)
op.sort(key=lambda x: -x[1])

i = 0

for m in range(M):
    b, c = op[m]
    for _ in range(b):
        if cards[i] < c:
            cards[i] = c
            i += 1
            if i >= N:
                break
        else:
            break
    else:
        continue
    break

print(sum(cards))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
import sys
import operator

n, m = map(int, input().split())

a = [int(x) for x in sys.stdin.readline().rstrip().split()]
a.sort()

cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(m)]
cards.sort(key=operator.itemgetter(1), reverse=True)

a_idx = 0
a_len = len(a)

for b, c in cards:
    if c <= a[a_idx] or a_idx == a_len:
        break
    for b_idx in range(b):
        if a_idx == a_len or c <= a[a_idx]:
            break
        else:
            a[a_idx] = c
            a_idx += 1

print(sum(a))


Fixed submission:
import sys
import operator

n, m = map(int, input().split())

a = [int(x) for x in sys.stdin.readline().rstrip().split()]
a.sort()

cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(m)]
cards.sort(key=operator.itemgetter(1), reverse=True)

a_idx = 0
a_len = len(a)

for b, c in cards:
    if a_idx == a_len:
        break
    for b_idx in range(b):
        if a_idx == a_len or c <= a[a_idx]:
            break
        else:
            a[a_idx] = c
            a_idx += 1

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

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

N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
B = []
C = []
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
    CB.append((c, b))
CB.sort()
for c, b in CB:
    for j in range(b):
        tmp = heapq.heappop(A)
        if tmp >= c:
            heapq.heappush(A, tmp)
            break
        else:
            heapq.heappush(A, c)
print(sum(A))


Fixed submission:
import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
B = []
C = []
CB = []
for _ in range(M):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
    CB.append((c, b))
CB.sort(reverse=True)
for c, b in CB:
    for j in range(b):
        tmp = heapq.heappop(A)
        if tmp >= c:
            heapq.heappush(A, tmp)
            break
        else:
            heapq.heappush(A, c)
print(sum(A))
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
        else:
            break
    if cnt >= N:
        break
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
print(sum(A))
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc.append([b, c])
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
cnt = 0
for i in range(m):
    for j in range(bc[i][0]):
        if bc[i][1] > a[cnt]:
            a[cnt] = bc[i][1]
            if cnt == n:
                break
            cnt += 1
        else:
            break
print(sum(a))


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc.append([b, c])
a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
cnt = 0
for i in range(m):
    for j in range(bc[i][0]):
        if bc[i][1] > a[cnt]:
            a[cnt] = bc[i][1]
            if cnt == n - 1:
                break
            cnt += 1
        else:
            break
print(sum(a))
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
BC = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
for i in range(M):
    A = A + [BC[i][1]] * BC[i][0]
    if len(A) >= 2 * N:
        break

A.sort(reverse=True)
print(sum(A[:N]))


Fixed submission:
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
BC = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
for i in range(M):
    A.extend([BC[i][1]] * BC[i][0])
    if len(A) >= 2 * N:
        break

A.sort(reverse=True)
print(sum(A[:N]))
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
template <typename F, typename S>
std::ostream &operator<<(ostream &os, const pair<F, S> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (const T &a : v) {
    os << a << ", ";
  }
  os << "]";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
  os << "{";
  for (const auto &p : m) {
    os << p.first << ":" << p.second << ", ";
  }
  os << "]";
  return os;
}

int main() {
  LL n, m;
  cin >> n >> m;
  vector<LL> a(n);
  for (LL i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<pair<LL, LL>> cmd(m);
  for (LL i = 0; i < m; ++i) {
    cin >> cmd[i].second >> cmd[i].first;
  }
  sort(cmd.begin(), cmd.end(), greater<pair<LL, LL>>());
  vector<LL> b;
  for (auto p : cmd) {
    for (LL i = 0; i < p.second; ++i) {
      if (b.size() >= n)
        break;
      b.push_back(p.first);
    }
  }
  sort(a.begin(), a.end());

  LL ans = 0;
  for (LL i = 0; i < n; ++i) {
    ans += max<LL>(a[i], b[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
template <typename F, typename S>
std::ostream &operator<<(ostream &os, const pair<F, S> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (const T &a : v) {
    os << a << ", ";
  }
  os << "]";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
  os << "{";
  for (const auto &p : m) {
    os << p.first << ":" << p.second << ", ";
  }
  os << "]";
  return os;
}

int main() {
  LL n, m;
  cin >> n >> m;
  vector<LL> a(n);
  for (LL i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<pair<LL, LL>> cmd(m);
  for (LL i = 0; i < m; ++i) {
    cin >> cmd[i].second >> cmd[i].first;
  }
  sort(cmd.begin(), cmd.end(), greater<pair<LL, LL>>());
  vector<LL> b;
  for (auto p : cmd) {
    for (LL i = 0; i < p.second; ++i) {
      if (b.size() >= n)
        break;
      b.push_back(p.first);
    }
  }
  sort(a.begin(), a.end());

  LL ans = 0;
  for (LL i = 0; i < n; ++i) {
    if (i < b.size()) {
      ans += max<LL>(a[i], b[i]);
    } else {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from collections import defaultdict
from collections import deque

input = sys.stdin.readline

new_cards = defaultdict(int)
n, m = map(int, input().split())
A = deque(sorted(list(map(int, input().split()))))

for i in range(m):
    b, c = map(int, input().split())
    new_cards[c] += b

for card, count in sorted(new_cards.items(), reverse=True):
    for i in range(count):
        if A[0] < card:
            A.append(card)
            A.popleft()

print(sum(A))


Fixed submission:
import sys
from collections import defaultdict
from collections import deque

input = sys.stdin.readline

new_cards = defaultdict(int)
n, m = map(int, input().split())
A = deque(sorted(list(map(int, input().split()))))

for i in range(m):
    b, c = map(int, input().split())
    new_cards[c] += b

for card, count in sorted(new_cards.items(), reverse=True):
    for i in range(count):
        if A[0] < card:
            A.append(card)
            A.popleft()
        else:
            print(sum(A))
            exit()

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

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

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

  vector<pair<ll, ll>> w(m + 100100);
  for (ll i = m; i < m + 100100; i++) {
    w[i].second = 0;
  }

  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    w[m + a].first = a;
    w[m + a].second++;
  }

  for (ll i = 0; i < m; i++) {
    cin >> w[i].second >> w[i].first;
  }

  sort(w.begin(), w.end());
  ll i = w.size() - 1;
  ll cnt = 0;
  ll sum = 0;
  bool f = false;
  while (true) {
    while (w[i].second > 0) {
      sum += w[i].first;
      cnt++;
      if (cnt == n) {
        f = true;
        break;
      }
      w[i].second--;
    }
    if (f) {
      break;
    }
    i--;
  }

  cout << sum << endl;
}


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

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

  vector<pair<ll, ll>> w(m + 100100);
  for (ll i = m; i < m + 100100; i++) {
    w[i].second = 0;
  }

  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    w[m + i].first = a;
    w[m + i].second++;
  }

  for (ll i = 0; i < m; i++) {
    cin >> w[i].second >> w[i].first;
  }

  sort(w.begin(), w.end());
  ll i = w.size() - 1;
  ll cnt = 0;
  ll sum = 0;
  bool f = false;
  while (true) {
    while (w[i].second > 0) {
      sum += w[i].first;
      cnt++;
      if (cnt == n) {
        f = true;
        break;
      }
      w[i].second--;
    }
    if (f) {
      break;
    }
    i--;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAXX 100005
#define PI 3.14159265358979323846264338327950
#define ll signed long long int
using namespace std;
ll t, n, a[MAXX], b[MAXX], c[MAXX];
vector<ll> adj[MAXX];
vector<pair<int, int>> v;
bool used[MAXX];
multiset<int> ms;

signed main() {
  FAST;
  ll r;
  ll m;
  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ms.insert(a[i]);
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i] >> c[i];
    sort(a, a + n);
    v.push_back({c[i], b[i]});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  ll ans = 0;
  for (ll i = 0; i < v.size(); i++) {
    ll x = v[i].first, y = v[i].second;
    while (*(ms.begin()) < x && y) {
      ms.erase(ms.begin());
      ms.insert(x);
      y--;
    }
  }

  while (n) {
    ans += *(ms.begin());
    ms.erase(ms.begin());
    n--;
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAXX 100005
#define PI 3.14159265358979323846264338327950
#define ll signed long long int
using namespace std;
ll t, n, a[MAXX], b[MAXX], c[MAXX];
vector<ll> adj[MAXX];
vector<pair<int, int>> v;
bool used[MAXX];
multiset<int> ms;

signed main() {
  FAST;
  ll r;
  ll m;
  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ms.insert(a[i]);
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i] >> c[i];
    v.push_back({c[i], b[i]});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  ll ans = 0;
  for (ll i = 0; i < v.size(); i++) {
    ll x = v[i].first, y = v[i].second;
    while (*(ms.begin()) < x && y) {
      ms.erase(ms.begin());
      ms.insert(x);
      y--;
    }
  }

  while (n) {
    ans += *(ms.begin());
    ms.erase(ms.begin());
    n--;
  }

  cout << ans;
}