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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  ll ans = 0;
  vector<pair<ll, ll>> ar(n);
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i], ans += a[i];
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> ar[i].second >> ar[i].first;
  }
  sort(ar.begin(), ar.end());
  reverse(ar.begin(), ar.end());
  int p = 0;
  for (int i = 0; i < m; i++) {
    while (p < n && ar[i].second && (ar[i].first > a[p])) {
      ans += ar[i].first - a[p];
      p++;
      ar[i].second--;
    }
  }
  cout << ans;
  return 0;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  ll ans = 0;
  vector<pair<ll, ll>> ar(m);
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i], ans += a[i];
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> ar[i].second >> ar[i].first;
  }
  sort(ar.begin(), ar.end());
  reverse(ar.begin(), ar.end());
  int p = 0;
  for (int i = 0; i < m; i++) {
    while (p < n && ar[i].second && (ar[i].first > a[p])) {
      ans += ar[i].first - a[p];
      p++;
      ar[i].second--;
    }
  }
  cout << ans;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#if 0

#endif

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
template <class A> void p(A a) { cout << (a) << endl; }
template <class A, class B> void p(A a, B b) {
  cout << a << " ";
  p(b);
}
template <class A, class B, class C> void p(A a, B b, C c) {
  cout << a << " ";
  p(b, c);
}
template <class A, class B, class C, class D> void p(A a, B b, C c, D d) {
  cout << a << " ";
  p(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for copy
// for(int i=0;i<N;++i) {

bool sortBySec(pair<int, ll> &a, pair<int, ll> &b) {
  return (a.second > b.second);
}

int main(void) {
  int N, M;
  cin >> N >> M;
  ll A[N];
  REP(i, N) { cin >> A[i]; }

  vector<pair<int, ll>> Buf;

  REP(i, M) {
    int B, C;
    cin >> B >> C;
    Buf.push_back(make_pair(B, C));
  }

  sort(A, A + N);

  sort(Buf.begin(), Buf.end(), sortBySec);

  // p("=========");

  vector<ll> D;

  REP(i, M) {
    REP(j, Buf[i].first) {
      if (D.size() <= N)
        D.push_back(Buf[i].second);
      else
        break;
    }
    if (D.size() >= N) {
      break;
    }
  }

  sort(D.begin(), D.end(), greater<ll>());

  // REP(i,D.size()) {
  //   p(D[i]);
  // }

  ll sum = 0;
  REP(i, N) {
    // p(D[i], A[i]);
    if (D[i] > A[i]) {
      sum += D[i];
    } else {
      sum += A[i];
    }
  }
  p(sum);
  return 0;
}


Fixed submission:
#if 0

#endif

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
template <class A> void p(A a) { cout << (a) << endl; }
template <class A, class B> void p(A a, B b) {
  cout << a << " ";
  p(b);
}
template <class A, class B, class C> void p(A a, B b, C c) {
  cout << a << " ";
  p(b, c);
}
template <class A, class B, class C, class D> void p(A a, B b, C c, D d) {
  cout << a << " ";
  p(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for copy
// for(int i=0;i<N;++i) {

bool sortBySec(pair<int, ll> &a, pair<int, ll> &b) {
  return (a.second > b.second);
}

int main(void) {
  int N, M;
  cin >> N >> M;
  ll A[N];
  REP(i, N) { cin >> A[i]; }

  vector<pair<int, ll>> Buf;

  REP(i, M) {
    int B, C;
    cin >> B >> C;
    Buf.push_back(make_pair(B, C));
  }

  sort(A, A + N);

  sort(Buf.begin(), Buf.end(), sortBySec);

  // p("=========");

  vector<ll> D;

  REP(i, M) {
    REP(j, Buf[i].first) {
      if (D.size() <= N)
        D.push_back(Buf[i].second);
      else
        break;
    }
    if (D.size() >= N) {
      break;
    }
  }

  sort(D.begin(), D.end(), greater<ll>());

  // REP(i,D.size()) {
  //   p(D[i]);
  // }

  ll sum = 0;
  REP(i, N) {
    // p(D[i], A[i]);
    if (i < D.size() && D[i] > A[i]) {
      sum += D[i];
    } else {
      sum += A[i];
    }
  }
  p(sum);
  return 0;
}
Problem p03038 (Python)

Buggy submission (status: Runtime Error):
def main():
    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 = sorted(BC, key=lambda x: x[1], reverse=True)
    i = 0
    for b, c in BC:
        for _ in range(b):
            if A[i] >= c:
                break
            A[i] = c
            i += 1
    print(sum(A))


main()


Fixed submission:
def main():
    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 = sorted(BC, key=lambda x: x[1], reverse=True)
    i = 0
    for b, c in BC:
        for _ in range(b):
            if i >= len(A) or A[i] >= c:
                break
            A[i] = c
            i += 1
    print(sum(A))


main()
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  // input
  int n, m;
  cin >> n >> m;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int tmp_a;
    cin >> tmp_a;
    a.push_back(tmp_a);
  }
  sort(a.begin(), a.end());
  map<int, int> bc;
  for (int i = 0; i < m; ++i) {
    int tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    auto itr = bc.find(tmp_c);
    if (itr == bc.end()) {
      bc[tmp_c] = tmp_b;
    } else {
      itr->second += tmp_b;
    }
  }
  bool flag = true;
  int pt = 0;
  for (auto itr = bc.rbegin(); itr != bc.rend(); ++itr) {
    int b = itr->second;
    int c = itr->first;
    for (int i = 0; i < b; i++) {
      if (a[pt] < c) {
        a[pt] = c;
        pt++;
      } else {
        flag = false;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  // input
  int n, m;
  cin >> n >> m;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int tmp_a;
    cin >> tmp_a;
    a.push_back(tmp_a);
  }
  sort(a.begin(), a.end());
  map<int, int> bc;
  for (int i = 0; i < m; ++i) {
    int tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    auto itr = bc.find(tmp_c);
    if (itr == bc.end()) {
      bc[tmp_c] = tmp_b;
    } else {
      itr->second += tmp_b;
    }
  }
  bool flag = true;
  int pt = 0;
  for (auto itr = bc.rbegin(); itr != bc.rend(); ++itr) {
    int b = itr->second;
    int c = itr->first;
    for (int i = 0; i < b; i++) {
      if (a[pt] < c) {
        a[pt] = c;
        pt++;
        if (pt == a.size()) {
          flag = false;
          break;
        }
      } else {
        flag = false;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (Python)

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

N, M = map(int, input().split())
# Aは昇順に並べる
A = list(sorted(map(int, input().split())))

# B,Cの対応辞書を作り、C順に並べる
counter = defaultdict(int)
for _ in range(M):
    B, C = map(int, input().split())
    counter[C] += B
c_list = list(sorted(counter.keys(), reverse=True))

# Cを大きい順に個数分並べたリストを作る
moves = []
n = 0
for c in c_list:
    b = counter[c]
    if n + b < N:
        p = b
    else:
        p = N - n
    for i in range(p):
        moves.append(c)

# AとCを小さい順に比較
siz = min(N, len(moves))
for i in range(siz):
    # Aの小さいものを上書き
    A[i] = max(A[i], moves[i])

print(sum(A))


Fixed submission:
from collections import defaultdict

N, M = map(int, input().split())
# Aは昇順に並べる
A = list(sorted(map(int, input().split())))

# B,Cの対応辞書を作り、C順に並べる
counter = defaultdict(int)
for _ in range(M):
    B, C = map(int, input().split())
    counter[C] += B
c_list = list(sorted(counter.keys(), reverse=True))

# Cを大きい順に個数分並べたリストを作る
moves = []
n = 0
for c in c_list:
    b = counter[c]
    if n + b < N:
        p = b
    else:
        p = N - n
    for i in range(p):
        moves.append(c)
    if len(moves) >= N:
        break

# AとCを小さい順に比較
siz = min(N, len(moves))
for i in range(siz):
    # Aの小さいものを上書き
    A[i] = max(A[i], moves[i])

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 2e9
typedef long long ll;

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

  sort(ALL(a));
  sort(ALL(p), greater<pair<int, int>>());
  ll ans = 0;
  int j = 0;
  REP(i, n) {
    ans += max(a[i], p[j].first);
    p[j].second--;
    if (p[j].second == 0)
      j++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 2e9
typedef long long ll;

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

  sort(ALL(a));
  sort(ALL(p), greater<pair<int, int>>());
  ll ans = 0;
  int j = 0;
  REP(i, n) {
    if (j == p.size()) {
      ans += a[i];
      continue;
    }
    ans += max(a[i], p[j].first);
    p[j].second--;
    if (p[j].second == 0)
      j++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

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

int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> A(N), B(M), C(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.begin(), A.end());
  vector<pair<long long, long long>> p(M);
  for (int i = 0; i < M; i++)
    p[i] = make_pair(C[i], B[i]);
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  long long cnt = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < p[i].second; j++) {
      if (A[cnt] >= p[i].first) {
        goto abc;
      } else {
        A[cnt] = p[i].first;
        cnt++;
      }
    }
  }
abc:
  long long sum = 0;
  for (int i = 0; i < N; i++)
    sum += A[i];
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> s(100009);

int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> A(N), B(M), C(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.begin(), A.end());
  vector<pair<long long, long long>> p(M);
  for (int i = 0; i < M; i++)
    p[i] = make_pair(C[i], B[i]);
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  long long cnt = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < p[i].second; j++) {
      if (cnt >= N) {
        goto abc;
      } else if (A[cnt] >= p[i].first) {
        goto abc;
      } else {
        A[cnt] = p[i].first;
        cnt++;
      }
    }
  }
abc:
  long long sum = 0;
  for (int i = 0; i < N; i++)
    sum += A[i];
  cout << sum << endl;
}
Problem p03038 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;
const int mod = 1e9 + 7;
const ll INF = 1e18;
const int inf = 1e9;

bool sortbysecond(ii a, ii b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  vector<ii> q;
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    q.push_back(ii(b, c));
  }
  sort(q.begin(), q.end(), sortbysecond);
  int k = 0;
  for (int i = m - 1; i >= 0; i--) {
    int b = q[i].first;
    int c = q[i].second;
    for (int j = 0; j < b; j++) {
      if (a[k] < c) {
        a[k] = c;
        k++;
      }
    }
  }

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}

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


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

typedef long long ll;
typedef pair<int, int> ii;
const int mod = 1e9 + 7;
const ll INF = 1e18;
const int inf = 1e9;

bool sortbysecond(ii a, ii b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  vector<ii> q;
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    q.push_back(ii(b, c));
  }
  sort(q.begin(), q.end(), sortbysecond);
  int k = 0;
  for (int i = m - 1; i >= 0; i--) {
    int b = q[i].first;
    int c = q[i].second;
    for (int j = 0; j < b; j++) {
      if (a[k] >= c)
        break;
      a[k] = c;
      k++;
    }
  }

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}

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

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

using namespace std;

typedef long long int ll;
typedef vector<ll> V;
typedef vector<V> VV;

int main() {
  // Happy hacking!
  ll n, m;
  V a;
  ll replace_idx = 0;
  vector<pair<ll, ll>> bc;

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    ll t;
    cin >> t;
    a.push_back(t);
  }
  for (ll i = 0; i < m; i++) {
    ll bt, ct;
    cin >> bt >> ct;
    bc.push_back(make_pair(bt, ct));
  }

  sort(a.begin(), a.end());
  sort(bc.begin(), bc.end(),
       [](auto x, auto y) { return x.second > y.second; });

  for (auto itr = bc.begin(); itr != bc.end(); itr++) {
    ll n_replace = itr->first;
    for (ll i = 0; i < n_replace; i++) {
      if (a[replace_idx] >= itr->second)
        break;

      a[replace_idx] = itr->second;
      replace_idx++;
    }
  }

  ll res = 0;
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    res += *itr;
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef vector<ll> V;
typedef vector<V> VV;

int main() {
  // Happy hacking!
  ll n, m;
  V a;
  ll replace_idx = 0;
  vector<pair<ll, ll>> bc;

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    ll t;
    cin >> t;
    a.push_back(t);
  }
  for (ll i = 0; i < m; i++) {
    ll bt, ct;
    cin >> bt >> ct;
    bc.push_back(make_pair(bt, ct));
  }

  sort(a.begin(), a.end());
  sort(bc.begin(), bc.end(),
       [](auto x, auto y) { return x.second > y.second; });

  for (auto itr = bc.begin(); itr != bc.end(); itr++) {
    ll n_replace = itr->first;
    for (ll i = 0; i < n_replace; i++) {
      // cout << replace_idx << endl;
      if (replace_idx >= n)
        break;
      if (a[replace_idx] >= itr->second)
        break;

      a[replace_idx] = itr->second;
      replace_idx++;
    }
  }

  ll res = 0;
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    res += *itr;
  }

  cout << res << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
ll mod = 1000000007;
ll INF = 1000000099;

void secondsort(vector<pair<ll, ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    swap(v.at(i).first, v.at(i).second);
  }
  sort(v.begin(), v.end());
  for (ll i = 0; i < v.size(); i++) {
    swap(v.at(i).first, v.at(i).second);
  }
}

signed main() {
  ll n, m, ans = 0;
  cin >> n >> m;

  priority_queue<ll, vector<ll>, greater<ll>> q;

  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    q.push(x);
  }

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

  for (ll i = 0; i < m; i++) {
    cin >> v.at(i).fi >> v.at(i).se;
  }
  secondsort(v);

  /*for(int i=0;i < m;i++)
  {
      ll l=0,r=v.at(i).fi-1;
      while(abs(r-l)>1){
          ll mid=(l+r)/2;
          if(q.at(mid)>v.at(i).se){
              r=mid;
          }else{
              l=mid;
          }
      }
      for(int j=0;j <=l ;j++)
      {
          q.at(0)=v.at(i).se;
      }
  }*/
  bool flag = false;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < v.at(i).fi; j++) {
      if (v.at(i).se > (ll)q.top()) {
        q.pop();
        q.push(v.at(i).se);
      } else {
        break;
        flag = true;
      }
    }
    if (flag) {
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    ans += q.top();
    q.pop();
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
ll mod = 1000000007;
ll INF = 1000000099;

void secondsort(vector<pair<ll, ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    swap(v.at(i).first, v.at(i).second);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (ll i = 0; i < v.size(); i++) {
    swap(v.at(i).first, v.at(i).second);
  }
}

signed main() {
  ll n, m, ans = 0;
  cin >> n >> m;

  priority_queue<ll, vector<ll>, greater<ll>> q;

  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    q.push(x);
  }

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

  for (ll i = 0; i < m; i++) {
    cin >> v.at(i).fi >> v.at(i).se;
  }
  secondsort(v);

  /*for(int i=0;i < m;i++)
  {
      ll l=0,r=v.at(i).fi-1;
      while(abs(r-l)>1){
          ll mid=(l+r)/2;
          if(q.at(mid)>v.at(i).se){
              r=mid;
          }else{
              l=mid;
          }
      }
      for(int j=0;j <=l ;j++)
      {
          q.at(0)=v.at(i).se;
      }
  }*/
  bool flag = false;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < v.at(i).fi; j++) {
      if (v.at(i).se > (ll)q.top()) {
        q.pop();
        q.push(v.at(i).se);
      } else {
        break;
        flag = true;
      }
    }
    if (flag) {
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    ans += q.top();
    q.pop();
  }

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

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

typedef pair<long long int, long long int> change;

bool pairCompare(const change &x, const change &y) {
  return x.second > y.second;
}

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

  vector<change> bc(m);

  for (int i = 0; i < m; i++) {
    scanf("%d", &bc[i].first);
    scanf("%d", &bc[i].second);
  }

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

  int prog = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < bc[i].first; j++) {
      if (prog < n) {
        if (a[prog] >= bc[i].second)
          break;
        a[prog] = bc[i].second;
        prog++;
      }
    }
  }

  long long int ans = 0;
  /*
  for (auto itr = a.begin(); itr != a.end(); itr++) {
          ans += *itr;
  }*/

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

  cout << ans << endl;

  return 0;
}

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

typedef pair<long long int, long long int> change;

bool pairCompare(const change &x, const change &y) {
  return x.second > y.second;
}

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

  vector<change> bc(m);

  for (int i = 0; i < m; i++) {
    scanf("%d", &bc[i].first);
    scanf("%d", &bc[i].second);
  }

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

  int prog = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < bc[i].first; j++) {
      // if (prog < n) {
      if (a[prog] >= bc[i].second)
        break;
      a[prog] = bc[i].second;
      prog++;
      //}
    }
  }

  long long int ans = 0;
  /*
  for (auto itr = a.begin(); itr != a.end(); itr++) {
          ans += *itr;
  }*/

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

  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

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

ll n, m;

int main() {
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<P> bc(n);
  rep(i, m) { cin >> bc[i].second >> bc[i].first; }
  sort(a.begin(), a.end());
  sort(bc.rbegin(), bc.rend());
  ll bcind = 0;
  rep(i, n) {
    if (a[i] >= bc[bcind].first)
      break;
    a[i] = bc[bcind].first;
    bc[bcind].second--;
    if (bc[bcind].second == 0)
      bcind++;
    if (bcind == m)
      break;
  }
  ll ans = 0;
  rep(i, n) { ans += a[i]; }
  cout << ans << endl;
  return 0;
}


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

ll n, m;

int main() {
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<P> bc(m);
  rep(i, m) { cin >> bc[i].second >> bc[i].first; }
  sort(a.begin(), a.end());
  sort(bc.rbegin(), bc.rend());
  ll bcind = 0;
  rep(i, n) {
    if (a[i] >= bc[bcind].first)
      break;
    a[i] = bc[bcind].first;
    bc[bcind].second--;
    if (bc[bcind].second == 0)
      bcind++;
    if (bcind == m)
      break;
  }
  ll ans = 0;
  rep(i, n) { ans += a[i]; }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

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

struct cardchange {
  long B;
  long C;
  cardchange(int B2, int C2) {
    B = B2;
    C = C2;
  }

  bool operator<(const cardchange &another) const { return C < another.C; }
};

int main() {
  long N, M;
  std::cin >> N >> M;
  std::vector<long> A;
  for (long i = 0; i < N; i++) {
    long temp;
    std::cin >> temp;
    A.push_back(temp);
  }
  std::sort(A.begin(), A.end());

  std::vector<cardchange> changes;
  for (long i = 0; i < M; i++) {
    long tempb, tempc;
    std::cin >> tempb >> tempc;
    changes.push_back(cardchange(tempb, tempc));
  }
  std::sort(changes.begin(), changes.end());

  long vs = M - 1;
  long changed_num = 0;
  for (long i = 0; i < N; i++) {
    if (A[i] < changes[vs].C && vs >= 0) {
      A[i] = changes[vs].C;
      changed_num++;
    }
    if (changed_num == changes[vs].B) {
      vs--;
      changed_num = 0;
    }
  }

  long sum = 0;
  for (long i = 0; i < N; i++) {

    sum += A[i];
  }
  std::cout << sum << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

struct cardchange {
  long B;
  long C;
  cardchange(int B2, int C2) {
    B = B2;
    C = C2;
  }

  bool operator<(const cardchange &another) const { return C < another.C; }
};

int main() {
  long N, M;
  std::cin >> N >> M;
  std::vector<long> A;
  for (long i = 0; i < N; i++) {
    long temp;
    std::cin >> temp;
    A.push_back(temp);
  }
  std::sort(A.begin(), A.end());

  std::vector<cardchange> changes;
  for (long i = 0; i < M; i++) {
    long tempb, tempc;
    std::cin >> tempb >> tempc;
    changes.push_back(cardchange(tempb, tempc));
  }
  std::sort(changes.begin(), changes.end());

  long vs = M - 1;
  long changed_num = 0;
  for (long i = 0; i < N; i++) {
    if (vs >= 0) {
      if (A[i] < changes[vs].C) {
        A[i] = changes[vs].C;
        changed_num++;
      }
      if (changed_num == changes[vs].B) {
        vs--;
        changed_num = 0;
      }
    }
  }

  long sum = 0;
  for (long i = 0; i < N; i++) {

    sum += A[i];
  }
  std::cout << sum << std::endl;
  return 0;
}
Problem p03038 (C++)

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

int main() {
  int64_t N, M;
  cin >> N >> M;
  int64_t v = 0;
  vector<int64_t> A(N);
  vector<vector<int>> X(N, vector<int>(2));
  for (int64_t i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  for (int64_t i = 0; i < M; i++) {
    cin >> X[i][1] >> X[i][0];
    v += X[i][1];
  }
  sort(X.begin(), X.end());
  reverse(X.begin(), X.end());
  v = min(v, N);
  vector<int64_t> D;
  for (int i = 0; i < N; i++) {
    if (D.size() == v)
      break;
    for (int j = 0; j < X[i][1]; j++) {
      D.push_back(X[i][0]);
      if (D.size() == v)
        break;
    }
  }
  /*for(int i=0;i<v;i++)
    cout<<D[i];*/
  int64_t answer = 0;
  for (int i = 0; i < N; i++) {
    if (i < v)
      answer += max(A[i], D[i]);
    else
      answer += A[i];
  }
  cout << answer;
}

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

int main() {
  int64_t N, M;
  cin >> N >> M;
  int64_t v = 0;
  vector<int64_t> A(N);
  vector<vector<int>> X(M, vector<int>(2));
  for (int64_t i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  for (int64_t i = 0; i < M; i++) {
    cin >> X[i][1] >> X[i][0];
    v += X[i][1];
  }
  sort(X.begin(), X.end());
  reverse(X.begin(), X.end());
  v = min(v, N);
  vector<int64_t> D;
  for (int i = 0; i < N; i++) {
    if (D.size() == v)
      break;
    for (int j = 0; j < X[i][1]; j++) {
      D.push_back(X[i][0]);
      if (D.size() == v)
        break;
    }
  }
  /*for(int i=0;i<v;i++)
    cout<<D[i];*/
  int64_t answer = 0;
  for (int i = 0; i < N; i++) {
    if (i < v)
      answer += max(A[i], D[i]);
    else
      answer += A[i];
  }
  cout << answer;
}
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);
  vector<pair<int, int>> bc(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

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

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

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

  int buf = 0;
  long long sum = 0;
  auto itr = bc.begin();
  for (int i = 0; i < n; i++) {
    if (itr->first <= a[i])
      break;
    a[i] = itr->first;
    if (--itr->second == 0)
      itr++;
  }
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }

  cout << sum << endl;

  return 0;
}


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

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

  vector<int> a(n);
  vector<pair<int, int>> bc(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

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

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

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

  int buf = 0;
  long long sum = 0;
  auto itr = bc.begin();
  for (int i = 0; i < n; i++) {
    if (itr->first <= a[i])
      break;
    a[i] = itr->first;
    if (--itr->second == 0)
      itr++;
    if (itr == bc.end())
      break;
  }
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }

  cout << sum << endl;

  return 0;
}
Problem p03038 (C++)

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

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());
  /*for(int i=0; i<n; i++){
      cout << a.at(i) << endl;
  }
  cout << endl;*/

  vector<tuple<int, int>> bc(m);
  int b, c;
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    bc.at(i) = make_tuple(c, b);
  }
  sort(bc.begin(), bc.end(), greater<>());
  /*for(int i=0; i<m; i++){
      cout << get<0>(bc.at(i)) << ' ' << get<1>(bc.at(i)) << endl;
  }
  cout << endl;*/

  int i = 0, j = 0;
  while (1) {
    if (a.at(i) < get<0>(bc.at(j))) {
      a.at(i) = get<0>(bc.at(j));
      get<1>(bc.at(j))--;
      if (get<1>(bc.at(j)) == 0)
        j++;
    }
    i++;
    if (i == n - 1)
      break;
    // cout << get<0>(bc.at(0)) << ' ' << get<1>(bc.at(0)) << endl;
    // cout << a.at(i) << endl;
  }

  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a.at(i);
    // cout << ans << endl;
  }
  cout << ans << endl;
}

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

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  sort(a.begin(), a.end());
  /*for(int i=0; i<n; i++){
      cout << a.at(i) << endl;
  }
  cout << endl;*/

  vector<tuple<int, int>> bc(m);
  int b, c;
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    bc.at(i) = make_tuple(c, b);
  }
  sort(bc.begin(), bc.end(), greater<>());
  /*for(int i=0; i<m; i++){
      cout << get<0>(bc.at(i)) << ' ' << get<1>(bc.at(i)) << endl;
  }
  cout << endl;*/

  int i = 0, j = 0;
  while (1) {
    if (a.at(i) < get<0>(bc.at(j))) {
      a.at(i) = get<0>(bc.at(j));
      get<1>(bc.at(j))--;
      if (get<1>(bc.at(j)) == 0)
        j++;
    }
    i++;
    if (i == n || j == m)
      break;
    // cout << get<0>(bc.at(0)) << ' ' << get<1>(bc.at(0)) << endl;
    // cout << a.at(i) << endl;
  }

  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a.at(i);
    // cout << ans << endl;
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<P> q;
  vector<int> a(n), b(m), c(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i] >> c[i];
  rep(i, n) q.push(make_pair(a[i], 1));
  rep(i, m) q.push(make_pair(c[i], b[i]));
  ll ans = 0;
  rep(i, n) {
    auto p = q.top();
    q.pop(); // 最大を取り除く
    ans += p.first;
    if (p.second > 1) {
      p.second--;
      q.push(p);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<P> q;
  vector<int> a(n), b(m), c(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i] >> c[i];
  rep(i, n) q.push(make_pair(a[i], 1));
  rep(i, m) q.push(make_pair(c[i], b[i]));
  ll ans = 0;
  rep(i, n) {
    auto p = q.top();
    q.pop(); // 最大を取り除く
    ans += p.first;
    if (p.second > 1) {
      p.second--;
      q.push(p);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

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

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  ll ans = 0;
  vector<P> d;
  rep(i, m) {
    int b, c;
    cin >> b >> c;

    d.push_back(make_pair(c, b));
  }
  sort(d.rbegin(), d.rend());

  int j = 0;
  rep(i, n) {
    if (d[j].second <= 0)
      j++;
    if (j >= n)
      break;
    if (a[i] < d[j].first) {
      a[i] = d[j].first;
      d[j].second--;
    }
    // cout << a[i] << "\n";
  }

  rep(i, n) { ans += a[i]; }

  cout << ans << "\n";

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

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

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  ll ans = 0;
  vector<P> d;
  rep(i, m) {
    int b, c;
    cin >> b >> c;

    d.push_back(make_pair(c, b));
  }
  sort(d.rbegin(), d.rend());

  int j = 0;
  rep(i, n) {
    if (d[j].second <= 0)
      j++;
    if (j >= m)
      break;
    if (a[i] < d[j].first) {
      a[i] = d[j].first;
      d[j].second--;
    }
    // cout << a[i] << "\n";
  }

  rep(i, n) { ans += a[i]; }

  cout << ans << "\n";

  return 0;
}
Problem p03038 (C++)

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

int a[100006];
struct lin {
  int num;
  int value;
};
lin b[100006];

bool cmp(lin a, lin b) { return a.value > b.value; }

int main() {
  int n;
  int m;
  cin >> n >> m;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    b[i].num = x;
    b[i].value = y;
  }
  sort(b, b + m, cmp);
  long long j = 0;
  long long k = 0;
  for (int i = 0; i < m; i++) {
    k = j + b[i].num;
    for (j; j < k; j++) {
      // cout<<"j="<<j<<" value="<<b[i].value<<" a[j]="<<a[j]<<endl;
      if (a[j] >= b[i].value) {
        k = -1;
        break;
      }
      sum = sum - a[j] + b[i].value;
    }
    if (k == -1)
      break;
  }
  cout << sum << endl;
  return 0;
}


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

int a[100006];
struct lin {
  int num;
  int value;
};
lin b[100006];

bool cmp(lin a, lin b) { return a.value > b.value; }

int main() {
  int n;
  int m;
  cin >> n >> m;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    b[i].num = x;
    b[i].value = y;
  }
  sort(b, b + m, cmp);
  long long j = 0;
  long long k = 0;
  for (int i = 0; i < m; i++) {
    k = j + b[i].num;
    for (j; j < k; j++) {
      // cout<<"j="<<j<<" value="<<b[i].value<<" a[j]="<<a[j]<<endl;
      if (a[j] >= b[i].value || j >= n) {
        k = -1;
        break;
      }
      sum = sum - a[j] + b[i].value;
    }
    if (k == -1)
      break;
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, a, b) for (ll i = a; i < b; i++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
void send(int a, int b, int c, int d) { printf("%d %d %d %d\n", a, b, c, d); }
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> v(n);
  rep(i, 0, n) { cin >> v[i]; }
  vector<ll> ans(n + 1, 0);
  rep(i, 0, n) { ans[i + 1] = ans[i] + v[i]; }
  sort(all(v));
  vector<P> p(m);
  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    p.pb(mp(y, x));
  }
  sort(all(p), greater<P>());
  int now = 0;
  ll sum = 0;
  rep(i, 0, m) {
    int st = i;
    rep(j, 0, p[i].se) {
      if (now >= n)
        break;
      if (p[i].fi > v[now]) {
        v[now] = p[i].fi;
        now++;
      }
    }
  }
  rep(i, 0, n) { sum += v[i]; }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, a, b) for (ll i = a; i < b; i++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
void send(int a, int b, int c, int d) { printf("%d %d %d %d\n", a, b, c, d); }
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> v(n);
  rep(i, 0, n) { cin >> v[i]; }
  vector<ll> ans(n + 1, 0);
  rep(i, 0, n) { ans[i + 1] = ans[i] + v[i]; }
  sort(all(v));
  vector<P> p(m);
  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    p.pb(mp(y, x));
  }
  sort(all(p), greater<P>());
  int now = 0;
  ll sum = 0;
  rep(i, 0, m) {
    int st = i;
    rep(j, 0, p[i].se) {
      if (now >= n)
        break;
      if (p[i].fi > v[now]) {
        v[now] = p[i].fi;
        now++;
      } else {
        break;
      }
    }
  }
  rep(i, 0, n) { sum += v[i]; }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
  int 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());
  reverse(CB.begin(), CB.end());
  ll ans = 0;
  ll tmp = 0;
  for (int i = 0; i < M; i++) {
    sort(A.begin(), A.end());
    auto itr = upper_bound(A.begin(), A.end(), CB[i].first);
    ll cnt = itr - A.begin();
    // cout << cnt << " " << CB[i].second << endl;
    if (cnt - tmp <= 0)
      continue;
    cnt = min(cnt - tmp, CB[i].second);
    ans += cnt * CB[i].first;
    tmp += cnt;
    // cout << tmp << endl;
  }
  for (int i = tmp; i < N; i++)
    ans += A[i];
  cout << ans << endl;
}

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

int main() {
  int 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());
  reverse(CB.begin(), CB.end());
  ll ans = 0;
  ll tmp = 0;
  for (int i = 0; i < M; i++) {
    auto itr = upper_bound(A.begin(), A.end(), CB[i].first);
    ll cnt = itr - A.begin();
    // cout << cnt << " " << CB[i].second << endl;
    if (cnt - tmp <= 0)
      continue;
    cnt = min(cnt - tmp, CB[i].second);
    ans += cnt * CB[i].first;
    tmp += cnt;
    // cout << tmp << endl;
  }
  for (int i = tmp; i < N; i++)
    ans += A[i];
  cout << ans << endl;
}
Problem p03038 (C++)

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

int main() {
  //
  long long num, que;

  cin >> num >> que;
  map<long long, long long> m;
  vector<pair<long long, long long>> p(que);
  for (long long i = 0; i < num; i++) {
    long long tmp;
    cin >> tmp;
    m[tmp]++;
  }

  for (long long i = 0; i < que; i++) {
    long long a, b;
    cin >> a >> b;
    p.at(i) = make_pair(b, a);
  }
  sort(p.rbegin(), p.rend());

  for (long long i = 0; i < que; i++) {
    long long cnt = 0;
    for (auto itr : m) {
      if (itr.second == 0)
        continue;
      if (p.at(i).second == 0 || itr.first >= p.at(i).first)
        continue;

      // cout << "itr:" << itr.first << ":" << itr.second << endl;
      // cout << "p  :" << p.at(i).first << ":" << p.at(i).second;
      if (p.at(i).second >= itr.second) {
        m[p.at(i).first] += itr.second;
        m[itr.first] = 0;
        p.at(i).second -= itr.second;
        // m.erase(begin(m));
        cnt++;
      } else {
        m[p.at(i).first] += p.at(i).second;
        m[itr.first] -= p.at(i).second;
        p.at(i).second = 0;
      }
      // cout << "->" << p.at(i).first << ":" << p.at(i).second << endl;
    }
    if (cnt > 0)
      m.erase(begin(m), next(begin(m), cnt));
  }

  long long kekka = 0;
  for (auto itr : m) {
    // cout << (long long)itr.first << "*" << (long long)itr.second << endl;
    kekka += (itr.first * itr.second);
  }

  cout << kekka << endl;
  //
}


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

int main() {
  //
  long long num, que;

  cin >> num >> que;
  map<long long, long long> m;
  vector<pair<long long, long long>> p(que);
  for (long long i = 0; i < num; i++) {
    long long tmp;
    cin >> tmp;
    m[tmp]++;
  }

  for (long long i = 0; i < que; i++) {
    long long a, b;
    cin >> a >> b;
    p.at(i) = make_pair(b, a);
  }
  sort(p.rbegin(), p.rend());

  for (long long i = 0; i < que; i++) {
    long long cnt = 0;
    for (auto itr : m) {
      if (itr.second == 0)
        continue;
      if (p.at(i).second == 0 || itr.first >= p.at(i).first)
        break;

      // cout << "itr:" << itr.first << ":" << itr.second << endl;
      // cout << "p  :" << p.at(i).first << ":" << p.at(i).second;
      if (p.at(i).second >= itr.second) {
        m[p.at(i).first] += itr.second;
        m[itr.first] = 0;
        p.at(i).second -= itr.second;
        // m.erase(begin(m));
        cnt++;
      } else {
        m[p.at(i).first] += p.at(i).second;
        m[itr.first] -= p.at(i).second;
        p.at(i).second = 0;
      }
      // cout << "->" << p.at(i).first << ":" << p.at(i).second << endl;
    }
    if (cnt > 0)
      m.erase(begin(m), next(begin(m), cnt));
  }

  long long kekka = 0;
  for (auto itr : m) {
    // cout << (long long)itr.first << "*" << (long long)itr.second << endl;
    kekka += (itr.first * itr.second);
  }

  cout << kekka << endl;
  //
}
Problem p03038 (C++)

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

int main() {

  long long N, M;
  cin >> N >> M;

  vector<int> A(N);
  vector<pair<int, int>> BC(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  long long bsum = 0;
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    bsum += b;
    pair<int, int> p = make_pair(c, b);
    BC.at(i) = p;
  }

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

  int j = min(N, bsum), k = 0;
  while (j > 0) {
    while (BC.at(k).second > 0) {
      A.push_back(BC.at(k).first);
      BC.at(k).second--;
      j--;
    }
    k++;
  }

  long long sum = 0;
  sort(A.begin(), A.end(), greater<int>());

  for (int i = 0; i < N; i++) {
    sum += A.at(i);
  }

  cout << sum << endl;
}


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

int main() {

  long long N, M;
  cin >> N >> M;

  vector<int> A(N);
  vector<pair<int, int>> BC(M);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  long long bsum = 0;
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    bsum += b;
    pair<int, int> p = make_pair(c, b);
    BC.at(i) = p;
  }

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

  int j = min(N, bsum), k = 0;
  while (j > 0) {
    while (BC.at(k).second > 0) {
      A.push_back(BC.at(k).first);
      BC.at(k).second--;
      j--;
    }
    k++;
  }

  long long sum = 0;
  sort(A.begin(), A.end(), greater<int>());

  for (int i = 0; i < N; i++) {
    sum += A.at(i);
  }

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

Buggy submission (status: Time Limit Exceeded):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  multiset<int> s;
  vector<vector<int>> v;

  int n, m;
  cin >> n >> m;
  rep(i, n) {
    int a;
    cin >> a;
    s.insert(a);
  }
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v.push_back({c, b});
  }
  for (auto x : v) {
    int i = 0;
    for (auto it = s.begin(); it != s.end();) {
      if (i == x[1]) {
        break;
      }
      if (x[0] > *it) {
        it = s.erase(it);
        s.insert(x[0]);
      } else {
        break;
      }
      i++;
    }
  }
  long long int t = 0;
  for (auto x : s) {
    t += (long long int)x;
  }
  cout << t;
}

Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  multiset<int> s;
  vector<vector<int>> v;

  int n, m;
  cin >> n >> m;
  rep(i, n) {
    int a;
    cin >> a;
    s.insert(a);
  }
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    v.push_back({c, b});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  // for(auto x:v){
  //     cout << x[0] << x[1] <<endl;
  // }
  for (auto x : v) {
    int i = 0;
    for (auto it = s.begin(); it != s.end();) {
      if (i == x[1]) {
        break;
      }
      if (x[0] > *it) {
        it = s.erase(it);
        s.insert(x[0]);
      } else {
        break;
      }
      i++;
    }
  }
  long long int t = 0;
  for (auto x : s) {
    t += (long long int)x;
  }
  cout << t;
}
Problem p03038 (C++)

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

int main() {
  int n, m;
  int a[100000];
  int b, c, i;
  long long int ans = 0;
  cin >> n >> m;

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

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

  sort(a, a + n);

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

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

  int count = 0;

  for (i = m - 1; count < n && i >= 0; i--) {
    for (int j = 0; j < pairs[i].second; j++) {
      if (a[count] < pairs[i].first) {
        a[count] = pairs[i].first;
        count++;
      } else {
        count = n;
      }
    }
  }

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

  cout << ans << endl;
}

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

int main() {
  int n, m;
  int a[100000];
  int b, c, i;
  long long int ans = 0;
  cin >> n >> m;

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

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

  sort(a, a + n);

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

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

  int count = 0;

  for (i = m - 1; count < n && i >= 0; i--) {
    for (int j = 0; j < pairs[i].second && count < n; j++) {
      if (a[count] < pairs[i].first) {
        a[count] = pairs[i].first;
        count++;
      } else {
        count = n;
      }
    }
  }

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

  cout << ans << endl;
}