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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;

int main() {
  priority_queue<ll, vector<ll>, greater<ll>> q;
  ll n, m, x, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x;
    q.push(x);
  }
  vector<pair<ll, ll>> v(m);
  for (int i = 0; i < m; i++)
    cin >> v[i].second >> v[i].first;
  sort(v.begin(), v.end(), greater<>());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < v[i].second; j++) {
      if (q.top() >= v[i].first)
        break;
      ans += v[i].first;
      q.pop();
    }
  }
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;

int main() {
  priority_queue<ll, vector<ll>, greater<ll>> q;
  ll n, m, x, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x;
    q.push(x);
  }
  vector<pair<ll, ll>> v(m);
  for (int i = 0; i < m; i++)
    cin >> v[i].second >> v[i].first;
  sort(v.begin(), v.end(), greater<>());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < v[i].second; j++) {
      if (q.empty() || q.top() >= v[i].first)
        break;
      ans += v[i].first;
      q.pop();
    }
  }
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define revrep(i, N) for (int i = N - 1; i >= 0; i--)
#define coutline cout << "------------ (" << __LINE__ << ")" << endl;

typedef long long ll;

int main(void) {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  vector<pair<int, int>> CB(N);

  rep(i, N) { cin >> A.at(i); }
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    CB.at(i) = make_pair(c, b);
  }

  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end(), std::greater<pair<int, int>>());

  ll ret = 0;
  auto itrA = A.begin();
  auto itrC = CB.begin();
  while (itrA != A.end()) {
    if (itrC != CB.end() && *itrA < itrC->first) {
      ret += itrC->first;
      itrC->second--;
      if (itrC->second <= 0)
        itrC++;
    } else {
      ret += *itrA;
    }
    itrA++;
  }

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define revrep(i, N) for (int i = N - 1; i >= 0; i--)
#define coutline cout << "------------ (" << __LINE__ << ")" << endl;

typedef long long ll;

int main(void) {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  vector<pair<int, int>> CB(M);

  rep(i, N) { cin >> A.at(i); }
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    CB.at(i) = make_pair(c, b);
  }

  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end(), std::greater<pair<int, int>>());

  ll ret = 0;
  auto itrA = A.begin();
  auto itrC = CB.begin();
  while (itrA != A.end()) {
    if (itrC != CB.end() && *itrA < itrC->first) {
      ret += itrC->first;
      itrC->second--;
      if (itrC->second <= 0)
        itrC++;
    } else {
      ret += *itrA;
    }
    itrA++;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <string>
using namespace std;
using ulli = long long int;

int main() {
  int n, m;
  cin >> n >> m;
  deque<ulli> cards;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    cards.push_back(temp);
  }
  sort(cards.begin(), cards.end());
  deque<pair<int, ulli>> bc;
  for (int i = 0; i < m; i++) {
    int b;
    ulli c;
    cin >> b >> c;
    bc.emplace_back(make_pair(b, c));
  }
  sort(bc.begin(), bc.end(), [](auto const &lhs, auto const &rhs) {
    return lhs.second > rhs.second;
  });
  ulli res = 0;
  deque<ulli>::iterator p = cards.begin();

  while (!bc.empty() && bc.front().second > *p) {
    *p = bc.front().second;
    bc.front().first--;
    p++;
    if (bc.front().first == 0) {
      bc.pop_front();
    }
  }
  while (!cards.empty()) {
    res += cards.front();
    cards.pop_front();
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <string>
using namespace std;
using ulli = long long int;

int main() {
  int n, m;
  cin >> n >> m;
  deque<ulli> cards;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    cards.push_back(temp);
  }
  sort(cards.begin(), cards.end());
  deque<pair<int, ulli>> bc;
  for (int i = 0; i < m; i++) {
    int b;
    ulli c;
    cin >> b >> c;
    bc.emplace_back(make_pair(b, c));
  }
  sort(bc.begin(), bc.end(), [](auto const &lhs, auto const &rhs) {
    return lhs.second > rhs.second;
  });
  ulli res = 0;
  deque<ulli>::iterator p = cards.begin();

  while (!bc.empty() && bc.front().second > *p && p != cards.end()) {
    *p = bc.front().second;
    bc.front().first--;
    p++;
    if (bc.front().first == 0) {
      bc.pop_front();
    }
  }
  while (!cards.empty()) {
    res += cards.front();
    cards.pop_front();
  }
  cout << res << endl;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;

struct BC {
  uint32_t B;
  uint32_t C;
};

int main() {
  uint32_t N, M;
  cin >> N >> M;
  vector<uint32_t> A;
  uint64_t sum = 0;
  for (uint32_t i = 0; i < N; ++i) {
    uint32_t t;
    cin >> t;
    A.push_back(t);
    sum += t;
  }
  sort(A.begin(), A.end());
  vector<BC> bc;

  for (uint32_t i = 0; i < M; i++) {
    uint32_t B, C;
    cin >> B >> C;
    bc.push_back(BC{B, C});
  }
  sort(bc.begin(), bc.end(), [](const BC &x, const BC y) { return x.C > y.C; });

  int k = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < bc[i].B; j++) {
      if (A[k] >= bc[i].C) {
        cout << sum << endl;
        return 0;
      }
      sum += bc[i].C - A[k];
      k++;
    }
  }
  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

struct BC {
  uint32_t B;
  uint32_t C;
};

int main() {
  uint32_t N, M;
  cin >> N >> M;
  vector<uint32_t> A;
  uint64_t sum = 0;
  for (uint32_t i = 0; i < N; ++i) {
    uint32_t t;
    cin >> t;
    A.push_back(t);
    sum += t;
  }
  sort(A.begin(), A.end());
  vector<BC> bc;

  for (uint32_t i = 0; i < M; i++) {
    uint32_t B, C;
    cin >> B >> C;
    bc.push_back(BC{B, C});
  }
  sort(bc.begin(), bc.end(), [](const BC &x, const BC y) { return x.C > y.C; });

  int k = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < bc[i].B; j++) {
      if (k >= N || A[k] >= bc[i].C) {
        cout << sum << endl;
        return 0;
      }
      sum += bc[i].C - A[k];
      k++;
    }
  }
  cout << sum << endl;
}
Problem p03038 (C++)

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

int main() {
  int n, m, b, c;
  cin >> n >> m;
  vector<int> a(n);
  vector<vector<int>> op(m);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    op.at(i) = {c, b};
  }
  sort(a.begin(), a.end());
  sort(op.begin(), op.end());
  reverse(op.begin(), op.end());

  int p = 0;
  for (int i = 0; i < m; i++) {
    b = op.at(i).at(1);
    c = op.at(i).at(0);
    while (a.at(p) < c && b > 0 && p < n) {
      a.at(p) = c;
      b--;
      p++;
    }
    if (b > 0)
      break;
  }

  int64_t ans = 0;
  for (int i = 0; i < n; i++)
    ans += a.at(i);

  cout << ans << endl;
}

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

int main() {
  int n, m, b, c;
  cin >> n >> m;
  vector<int> a(n);
  vector<vector<int>> op(m);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    op.at(i) = {c, b};
  }
  sort(a.begin(), a.end());
  sort(op.begin(), op.end());
  reverse(op.begin(), op.end());

  int p = 0;
  for (int i = 0; i < m; i++) {
    b = op.at(i).at(1);
    c = op.at(i).at(0);
    while (b > 0 && p < n &&
           a.at(p) < c) { // 判定の順序のせいでa[n]out_of_rangeになった
      a.at(p) = c;
      b--;
      p++;
    }
    if (b > 0)
      break;
  }

  int64_t ans = 0;
  for (int i = 0; i < n; i++)
    ans += a.at(i);

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

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

int main() {
  // input
  int n, m, a, b, c;
  cin >> n >> m;
  vector<pair<ll, int>> cb;
  rep(i, n) {
    cin >> a;
    cb.push_back(make_pair(a, 1));
  }
  rep(i, m) {
    cin >> b >> c;
    cb[i] = make_pair(c, b);
  }
  sort(cb.begin(), cb.end(), greater<pair<ll, int>>());

  // solve
  ll ans = 0;
  rep(i, cb.size()) {
    if (n > cb[i].second) {
      ans += cb[i].first * cb[i].second;
      n -= cb[i].second;
      if (n == 0)
        break;
    } else {
      ans += cb[i].first * n;
      break;
    }
  }
  cout << ans << endl;
}

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

int main() {
  // input
  int n, m, a, b, c;
  cin >> n >> m;
  vector<pair<ll, int>> cb;
  rep(i, n) {
    cin >> a;
    cb.push_back(make_pair(a, 1));
  }
  rep(i, m) {
    cin >> b >> c;
    cb.push_back(make_pair(c, b));
  }
  sort(cb.begin(), cb.end(), greater<pair<ll, int>>());

  // solve
  ll ans = 0;
  rep(i, cb.size()) {
    if (n > cb[i].second) {
      ans += cb[i].first * cb[i].second;
      n -= cb[i].second;
      if (n == 0)
        break;
    } else {
      ans += cb[i].first * n;
      break;
    }
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all                                                                    \
  int i = 0;                                                                   \
  i < n;                                                                       \
  i++
#define pb push_back
typedef long long int ll;

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl '\n'
#define mod 1000000007

bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}
vector<pair<ll, ll>> v;
int main() {
  FAST_IO;
  ll n, m, x, ans = 0;
  cin >> n >> m;
  ll arr[n];
  for (all)
    cin >> arr[i];
  sort(arr, arr + n);
  v.resize(n);
  for (int i = 0; i < m; i++)
    cin >> v[i].second >> v[i].first;
  sort(v.begin(), v.end(), sortinrev);
  int j = 0, flag = 0;
  for (int i = 0; i < m; i++) {
    if (flag == 1)
      break;
    while (true) {
      if (j >= n || v[i].second <= 0)
        break;
      if (arr[j] < v[i].first) {
        arr[j] = v[i].first;
        v[i].second--;
        j++;
      } else {
        flag = 1;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++)
    ans += arr[i];
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all                                                                    \
  int i = 0;                                                                   \
  i < n;                                                                       \
  i++
#define pb push_back
typedef long long int ll;

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl '\n'
#define mod 1000000007

bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}
vector<pair<ll, ll>> v;
int main() {
  FAST_IO;
  ll n, m, x, ans = 0;
  cin >> n >> m;
  ll arr[n];
  for (all)
    cin >> arr[i];
  sort(arr, arr + n);
  v.resize(m);
  for (int i = 0; i < m; i++)
    cin >> v[i].second >> v[i].first;
  sort(v.begin(), v.end(), sortinrev);
  int j = 0, flag = 0;
  for (int i = 0; i < m; i++) {
    if (flag == 1)
      break;
    while (true) {
      if (j >= n || v[i].second <= 0)
        break;
      if (arr[j] < v[i].first) {
        arr[j] = v[i].first;
        v[i].second--;
        j++;
      } else {
        flag = 1;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++)
    ans += arr[i];
  cout << ans << endl;
}
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++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}

int main() {
  ll N, M;
  cin >> N >> M;
  deque<ll> A;
  priority_queue<Pll> B;
  ll a, b, c;
  rep(i, N) {
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());
  rep(i, M) {
    cin >> b >> c;
    B.push(Pll(c, b));
  }
  ll cnt = 0;
  rep(i, M) {
    if (cnt > N)
      break;
    c = B.top().first;
    b = B.top().second;
    cnt += b;
    B.pop();
    rep(j, b) {
      auto itr = lower_bound(A.begin(), A.end(), c);
      if (itr == A.begin()) {
        break;
      } else {
        A.erase(A.begin());
        A.insert(itr, c);
      }
    }
  }
  ll sum = 0;
  rep(i, N) {
    // cout << A[i] << " ";
    sum += A[i];
  }
  // cout << endl;
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}

int main() {
  ll N, M;
  cin >> N >> M;
  deque<ll> A;
  priority_queue<Pll> B;
  ll a, b, c;
  rep(i, N) {
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());
  rep(i, M) {
    cin >> b >> c;
    B.push(Pll(c, b));
  }
  ll cnt = 0;
  rep(i, M) {
    if (cnt > N)
      break;
    c = B.top().first;
    b = B.top().second;
    cnt += b;
    B.pop();
    rep(j, b) {
      auto itr = lower_bound(A.begin(), A.end(), c);
      if (itr == A.begin()) {
        break;
      } else if (itr == A.end()) {
        A.erase(A.begin());
        A.insert(A.end(), c);
      } else {
        A.erase(A.begin());
        A.insert(itr, c);
      }
    }
  }
  ll sum = 0;
  rep(i, N) {
    // cout << A[i] << " ";
    sum += A[i];
  }
  // cout << endl;
  cout << sum << endl;
}
Problem p03038 (C++)

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;
const ll MOD = 1e9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  V<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

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

  sort(ALL(a));
  sort(ALL(pa), greater<P<ll, ll>>());

  int p = 0;
  for (int i = 0; i < m; i++) {
    if (n < p)
      break;
    ll b = pa[i].second;
    ll c = pa[i].first;
    if (a[p] <= c) {
      for (int j = p; j < p + b; j++) {
        if (a[j] <= c)
          a[j] = c;
      }
      p += b;
    }
  }

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


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;
const ll MOD = 1e9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  V<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

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

  sort(ALL(a));
  sort(ALL(pa), greater<P<ll, ll>>());

  int p = 0;
  for (int i = 0; i < m; i++) {
    if (n < p)
      break;
    ll b = pa[i].second;
    ll c = pa[i].first;
    if (a[p] <= c) {
      for (int j = p; j < min(p + b, n); j++) {
        if (a[j] <= c)
          a[j] = c;
      }
      p += b;
    }
  }

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

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

using namespace std;

typedef long long int64;

int main(void) {
  int64 N, M;
  cin >> N >> M;
  vector<int64> A(N);
  vector<pair<int64, int64>> CB(M);
  int64 ans = 0;

  for (int64 i = 0; i < N; i++) {
    cin >> A[i];
  }
  int64 b, c;
  for (int64 i = 0; i < M; i++) {
    cin >> b >> c;
    CB[i] = make_pair(c, b);
  }

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

  int64 j = 0;
  int64 tmp = 0;

  while (tmp <= N) {
    for (int64 i = 0; i < CB[j].second; i++) {
      A.push_back(CB[j].first);
      tmp++;
      // cout<<CB[j].first<<endl;
      if (tmp > N)
        break;
    }
    j++;
  }

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

  for (int64 i = 0; i < N; i++) {
    ans += A[i];
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

typedef long long int64;

int main(void) {
  int64 N, M;
  cin >> N >> M;
  vector<int64> A(N);
  vector<pair<int64, int64>> CB(M);
  int64 ans = 0;

  for (int64 i = 0; i < N; i++) {
    cin >> A[i];
  }
  int64 b, c;
  for (int64 i = 0; i < M; i++) {
    cin >> b >> c;
    CB[i] = make_pair(c, b);
  }

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

  int64 j = 0;
  int64 tmp = 0;

  while (tmp <= N) {
    for (int64 i = 0; i < CB[j].second; i++) {
      A.push_back(CB[j].first);
      tmp++;
      // cout<<CB[j].first<<endl;
      if (tmp > N)
        break;
    }
    j++;
    if (j >= M)
      break;
  }

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

  for (int64 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;

const int maxn = 1e5 + 10;

int a[maxn], n, m;
vector<pair<int, int>> v;

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++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v.push_back(make_pair(y, x));
  }
  int pos = 0;
  sort(a, a + n);
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    int x = v[i].first, y = v[i].second;
    int sum = y + pos;
    for (int i = pos; i < sum; i++) {
      if (a[i] > x)
        break;
      else
        a[i] = x;
      pos++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    //	cout<<a[i]<<endl;
    ans += a[i];
  }
  printf("%lld", ans);
  return 0;
}

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

const int maxn = 1e5 + 10;

int a[maxn], n, m;
vector<pair<int, int>> v;

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++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v.push_back(make_pair(y, x));
  }
  int pos = 0;
  sort(a, a + n);
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int now = -1, has = 0, x;
  for (int i = 0; i < n; i++) {
    if (has == 0) {
      now++;
      x = v[now].first;
      has = v[now].second;
    }
    if (a[i] > x)
      break;
    else {
      a[i] = x;
      has--;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    //	cout<<a[i]<<endl;
    ans += a[i];
  }
  printf("%lld", ans);
  return 0;
}
Problem p03038 (C++)

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

using ll = long long;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vdb = vector<db>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
#define rep(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define vrep(i, vec) for (auto &i : vec)
#define vin(vec)                                                               \
  for (auto &i : vec)                                                          \
  cin >> i
#define all(v) v.begin(), v.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll mod = 1000000007;
const ll inf = 1LL << 60;

int main() {
  ll n, m;
  cin >> n >> m;
  vll a(n);
  vin(a);
  vpll chn(m);
  rep(i, 0, m) {
    ll b, c;
    cin >> c >> b;
    chn.at(i) = make_pair(b, c);
  }

  sort(all(a));
  sort(all(chn), greater<pll>());
  ll now = 0;
  vrep(i, chn) {
    ll value = i.first, j = i.second;
    while (j > 0) {
      if (a.at(now) < value && now < n) {
        a.at(now) = value;
        j--;
        now++;
      } else
        goto out;
    }
  }
out:
  cout << accumulate(all(a), 0ll) << endl;
}

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

using ll = long long;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vdb = vector<db>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
#define rep(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define vrep(i, vec) for (auto &i : vec)
#define vin(vec)                                                               \
  for (auto &i : vec)                                                          \
  cin >> i
#define all(v) v.begin(), v.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll mod = 1000000007;
const ll inf = 1LL << 60;

int main() {
  ll n, m;
  cin >> n >> m;
  vll a(n);
  vin(a);
  vpll chn(m);
  rep(i, 0, m) {
    ll b, c;
    cin >> c >> b;
    chn.at(i) = make_pair(b, c);
  }

  sort(all(a));
  sort(all(chn), greater<pll>());
  ll now = 0;
  vrep(i, chn) {
    ll value = i.first, j = i.second;
    while (j > 0) {
      if (now < n && a.at(now) < value) {
        a.at(now) = value;
        j--;
        now++;
      } else
        goto out;
    }
  }
out:
  cout << accumulate(all(a), 0ll) << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define MaxSize 100005
ll n, m, sum = 0;
ll a[MaxSize];
P res[MaxSize];
void Solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> res[i].second >> res[i].first;
  sort(res, res + m, greater<P>());
  sort(a, a + n);
  int tmp = 0;
  for (int i = 0; i < m; i++) {
    int tmp1 = tmp;
    for (int j = tmp1; j < tmp1 + res[i].second; j++) {
      if (a[j] < res[i].first) {
        a[j] = res[i].first;
        tmp++;
      } else
        break;
    }
  }
  for (int i = 0; i < n; i++)
    sum += a[i];
  cout << sum << endl;
}
int main() {
  Solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define MaxSize 100005
ll n, m, sum = 0;
ll a[MaxSize];
P res[MaxSize];
void Solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> res[i].second >> res[i].first;
  sort(res, res + m, greater<P>());
  sort(a, a + n);
  int tmp = 0;
  for (int i = 0; i < m; i++) {
    int tmp1 = tmp;
    for (int j = tmp1; j < tmp1 + res[i].second; j++) {
      if (tmp >= n)
        break;
      else if (a[j] < res[i].first) {
        a[j] = res[i].first;
        tmp++;
      } else
        break;
    }
  }
  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 <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using P = pair<ll, ll>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<P> D;
  rep(i, M) {
    ll B, C;
    cin >> B >> C;
    D.push_back(make_pair(C, B));
  }

  sort(A.begin(), A.end());
  sort(D.rbegin(), D.rend());
  vector<ll> E;

  rep(i, M) {
    rep(j, D[i].second) {
      E.push_back(D[i].first);
      if (E.size() == N)
        break;
    }
  }

  ll ans = 0;
  rep(i, N) {
    if (i < E.size())
      ans += max(A[i], E[i]);
    else
      ans += A[i];
  }
  cout << ans << endl;

  return 0;
}
// Ctrl+Shift+Bでコンパイルと実行を行ってデバッグすること

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using P = pair<ll, ll>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<P> D;
  rep(i, M) {
    ll B, C;
    cin >> B >> C;
    D.push_back(make_pair(C, B));
  }

  sort(A.begin(), A.end());
  sort(D.rbegin(), D.rend());
  vector<ll> E;

  rep(i, M) {
    rep(j, D[i].second) {
      E.push_back(D[i].first);
      if (E.size() == N)
        break;
    }
    if (E.size() == N)
      break;
  }

  ll ans = 0;
  rep(i, N) {
    if (i < E.size())
      ans += max(A[i], E[i]);
    else
      ans += A[i];
  }
  cout << ans << endl;

  return 0;
}
// Ctrl+Shift+Bでコンパイルと実行を行ってデバッグすること
Problem p03038 (C++)

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

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

  vector<int> v;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());

  int b, c;
  vector<vector<int>> sousa;
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    vector<int> temp = {b, c};
    sousa.push_back(temp);
  }
  sort(sousa.begin(), sousa.end(),
       [](auto &x, auto &y) { return x[1] > y[1]; });

  vector<int>::iterator it = v.end();
  vector<int>::iterator be = v.begin();
  for (int i = 0; i < m; i++) {
    it = lower_bound(be, it, sousa[i][1]);
    if (it != be) {
      int count = 0;
      for (auto t = be; t != it; t++) {
        if (count < sousa[i][0]) {
          *t = sousa[i][1];
          count++;
          be++;
        } else {
          break;
        }
      }
    } else {
      break;
    }
    sort(be, it);
  }

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res += v[i];
  }

  cout << res << endl;
}

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

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

  vector<int> v;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());

  int b, c;
  vector<vector<int>> sousa;
  for (int i = 0; i < m; i++) {
    cin >> b >> c;
    vector<int> temp = {b, c};
    sousa.push_back(temp);
  }
  sort(sousa.begin(), sousa.end(),
       [](auto &x, auto &y) { return x[1] > y[1]; });

  vector<int>::iterator it = v.end();
  vector<int>::iterator be = v.begin();
  for (int i = 0; i < m; i++) {
    it = lower_bound(be, it, sousa[i][1]);
    if (it != be) {
      int count = 0;
      for (auto t = be; t != it; t++) {
        if (count < sousa[i][0]) {
          *t = sousa[i][1];
          count++;
          be++;
        } else {
          break;
        }
      }
    } else {
      break;
    }
  }

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res += v[i];
  }

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

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

struct Card {
  ll b;
  ll c;
  Card(ll b = 0, ll c = 0) {
    this->b = b;
    this->c = c;
  }
  bool operator<(const Card &a) const { return this->c < a.c; }
  void print() { printf("b:%2ld c:%2ld\n", this->b, this->c); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<Card> dic(m);
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    dic[i].b = b;
    dic[i].c = c;
  }
  sort(a.begin(), a.end());
  sort(dic.rbegin(), dic.rend());
  vector<ll> d;
  rep(i, m) rep(j, dic[i].b) d.push_back(dic[i].c);
  int ptr = 0;
  for (; ptr < d.size(); ptr++) {
    if (ptr >= n)
      break;
    if (a[ptr] > d[ptr])
      break;
  }
  ll ans = 0;
  if (ptr >= n) {
    rep(i, ptr) ans += d[i];
  } else {
    rep(i, ptr) ans += d[i];
    for (int i = ptr; i < n; i++)
      ans += a[i];
  }
  cout << ans << endl;
}


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

struct Card {
  ll b;
  ll c;
  Card(ll b = 0, ll c = 0) {
    this->b = b;
    this->c = c;
  }
  bool operator<(const Card &a) const { return this->c < a.c; }
  void print() { printf("b:%2ld c:%2ld\n", this->b, this->c); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<Card> dic(m);
  rep(i, m) {
    ll b, c;
    cin >> b >> c;
    dic[i].b = b;
    dic[i].c = c;
  }
  sort(a.begin(), a.end());
  sort(dic.rbegin(), dic.rend());
  vector<ll> d;
  int count = 0;
  rep(i, m) {
    rep(j, dic[i].b) {
      count++;
      d.push_back(dic[i].c);
      if (count > n)
        break;
    }
    if (count > n)
      break;
  }
  int ptr = 0;
  for (; ptr < d.size(); ptr++) {
    if (ptr >= n)
      break;
    if (a[ptr] > d[ptr])
      break;
  }
  ll ans = 0;
  if (ptr >= n) {
    rep(i, ptr) ans += d[i];
  } else {
    rep(i, ptr) ans += d[i];
    for (int i = ptr; i < n; i++)
      ans += a[i];
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

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.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> CB.at(i).second >> CB.at(i).first;
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  int k = 0;
  for (int i = 0; i < M; i++) {
    bool flag = false;
    for (int j = 0; j < CB.at(i).second; j++) {
      if (A.at(k) < CB.at(i).first) {
        A.at(k) = CB.at(i).first;
        k++;
      } else {
        flag = true;
        break;
      }
    }
    if (flag) {
      break;
    }
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A.at(i);
  }
  cout << ans << endl;
}


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

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.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> CB.at(i).second >> CB.at(i).first;
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  int k = 0;
  for (int i = 0; i < M; i++) {
    bool flag = false;
    for (int j = 0; j < CB.at(i).second; j++) {
      if (A.at(k) < CB.at(i).first) {
        A.at(k) = CB.at(i).first;
        k++;
        if (k == N) {
          flag = true;
          break;
        }
      } else {
        flag = true;
        break;
      }
    }
    if (flag) {
      break;
    }
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A.at(i);
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> p(n);

  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());

  //  rep(i, m) cin >> p[i].second >> p[i].first;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    p[i] = {c, b};
  }
  sort(p.rbegin(), p.rend());

  ll i = 0;
  ll j = 0;

  while (a[i] < p[j].first && i < n && j < m) {
    a[i] = p[j].first;
    i++;
    if (p[j].second > 1)
      p[j].second--;
    else
      j++;
  }

  // int l = 0;
  // for (auto pp : p) {
  //   int cnt = pp.second;
  //   while (cnt-- && a[l] < pp.first && l < n) {
  //     a[l++] = pp.first;
  //   }
  // }

  ll ans = 0;

  rep(i, n) ans += 1LL * a[i];
  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;
typedef long long ll;
const int INF = 1e9;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<pair<int, int>> p(m);

  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());

  //  rep(i, m) cin >> p[i].second >> p[i].first;
  rep(i, m) {
    int b, c;
    cin >> b >> c;
    p[i] = {c, b};
  }
  sort(p.rbegin(), p.rend());

  ll i = 0;
  ll j = 0;

  while (a[i] < p[j].first && i < n && j < m) {
    a[i] = p[j].first;
    i++;
    if (p[j].second > 1)
      p[j].second--;
    else
      j++;
  }

  // int l = 0;
  // for (auto pp : p) {
  //   int cnt = pp.second;
  //   while (cnt-- && a[l] < pp.first && l < n) {
  //     a[l++] = pp.first;
  //   }
  // }

  ll ans = 0;

  rep(i, n) ans += 1LL * a[i];
  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
using namespace std;

/*
int main(){
   int a,b;cin>>a>>b;
   if(a>=13) cout<<b<<endl;
   else if(a>=6 and a<=12) cout<<b/2<<endl;
   else cout<<0<<endl;
 //  //return 0;
    return 0;
}*/
vector<int> box1(1000000000, 0);
vector<int> box2(1000000000, 0);
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N, 0);
  ll amax = -1;
  ll ans = 0;
  set<int> zaatu;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++) {
    box1[A[i]]++;
    // amax = max(amax,A[i]);
    ans += A[i];
    zaatu.insert(A[i]);
  }
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    box2[c] += b;
    zaatu.insert(c);
  }
  auto itr1 = zaatu.begin(); // int now1 = *itr1;
  auto itr2 = zaatu.end();
  itr2--; // int now2 = *itr2;
  while (itr1 != itr2) {
    if (box1[*itr1] == 0) {
      itr1++;
      continue;
    }
    if (box2[*itr2] == 0) {
      itr2--;
      continue;
    }
    box1[*itr1]--;
    box2[*itr2]--;
    ans = ans + *itr2 - *itr1;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
using namespace std;

/*
int main(){
   int a,b;cin>>a>>b;
   if(a>=13) cout<<b<<endl;
   else if(a>=6 and a<=12) cout<<b/2<<endl;
   else cout<<0<<endl;
 //  //return 0;
    return 0;
}*/
map<int, int> box1; //(1000000000,0);
map<int, int> box2; //(1000000000,0);
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N, 0);
  ll amax = -1;
  ll ans = 0;
  set<int> zaatu;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++) {
    box1[A[i]]++;
    // amax = max(amax,A[i]);
    ans += A[i];
    zaatu.insert(A[i]);
  }
  for (int i = 0; i < M; i++) {
    int b, c;
    cin >> b >> c;
    box2[c] += b;
    zaatu.insert(c);
  }
  auto itr1 = zaatu.begin(); // int now1 = *itr1;
  auto itr2 = zaatu.end();
  itr2--; // int now2 = *itr2;
  while (itr1 != itr2) {
    if (box1[*itr1] == 0) {
      itr1++;
      continue;
    }
    if (box2[*itr2] == 0) {
      itr2--;
      continue;
    }
    box1[*itr1]--;
    box2[*itr2]--;
    ans = ans + *itr2 - *itr1;
  }

  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> CB(M);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < M; i++) {
    int B, C;
    cin >> B >> C;
    CB.at(i) = make_pair(C, B);
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  vector<int> vec;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < CB.at(i).second; j++) {
      vec.push_back(CB.at(i).first);
    }
  }

  int i = 0;
  int j = 0;
  while (i < N && j < vec.size()) {
    if (A.at(i) < vec.at(j)) {
      A.at(i) = vec.at(j);
      i++;
    }
    j++;
  }

  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans += A.at(i);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> CB(M);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < M; i++) {
    int B, C;
    cin >> B >> C;
    CB.at(i) = make_pair(C, B);
  }
  sort(A.begin(), A.end());
  sort(CB.begin(), CB.end());
  reverse(CB.begin(), CB.end());

  vector<int> vec;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < CB.at(i).second; j++) {
      if (vec.size() > N)
        break;
      vec.push_back(CB.at(i).first);
    }
  }

  int i = 0;
  int j = 0;
  while (i < N && j < vec.size()) {
    if (A.at(i) < vec.at(j)) {
      A.at(i) = vec.at(j);
      i++;
    }
    j++;
  }

  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans += A.at(i);
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;

int main() {
  ll i, j, k, n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<pair<ll, ll>> q(n + 1);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < m; i++) {
    cin >> q[i].second >> q[i].first;
  }
  // sort(a.begin(),a.end());
  sort(q.begin(), q.end());
  reverse(q.begin(), q.end());
  vector<ll> change(n);
  ll now = 0; // 置き換えたところ
  priority_queue<ll> que;
  ll qs = q.size();
  for (i = 0; i < qs; i++) {
    for (j = 0; j < q[i].second; j++) {
      // change[now++]=q[i].first;
      que.push(q[i].first);
      now++;
      if (now == n)
        break;
    }
    if (now == n)
      break;
  }
  // sort(change.begin(),change.end());
  // reverse(change.begin(),change.end());
  // ll s = change.size();
  for (i = 0; i < n; i++) {
    que.push(a[i]);
  }
  ll ret = 0;
  // for(i=0;i<s;i++)que.push(change[i]);
  for (i = 0; i < n; i++) {
    ret += que.top();
    que.pop();
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;

int main() {
  ll i, j, k, n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<pair<ll, ll>> q(m);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < m; i++) {
    cin >> q[i].second >> q[i].first;
  }
  // sort(a.begin(),a.end());
  sort(q.begin(), q.end());
  reverse(q.begin(), q.end());
  vector<ll> change(n);
  ll now = 0; // 置き換えたところ
  priority_queue<ll> que;
  ll qs = q.size();
  for (i = 0; i < qs; i++) {
    for (j = 0; j < q[i].second; j++) {
      // change[now++]=q[i].first;
      que.push(q[i].first);
      now++;
      if (now == n)
        break;
    }
    if (now == n)
      break;
  }
  // sort(change.begin(),change.end());
  // reverse(change.begin(),change.end());
  // ll s = change.size();
  for (i = 0; i < n; i++) {
    que.push(a[i]);
  }
  ll ret = 0;
  // for(i=0;i<s;i++)que.push(change[i]);
  for (i = 0; i < n; i++) {
    ret += que.top();
    que.pop();
  }
  cout << ret << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define int long long
#define mod (int)(1000000007)
using namespace std;
int test[213456];

signed main() {
  int n, k, b = 0, ans = 0;
  cin >> n >> k;
  int a = k - 1;
  for (int h = 0; h < n; h++) {
    cin >> test[h];
  }
  sort(test, test + n);
  vector<vector<long long>> aa(k, vector<long long>(2));
  for (int h = 0; h < k; h++) {
    cin >> aa[h][1] >> aa[h][0];
  }
  sort(aa.begin(), aa.end());
  for (int h = 0; h < n; h++) {
    if (b != 0 && test[h] < aa[a][0]) {
      test[h] = aa[a][0];
      b--;
      if (a == 0 && b == 0) {
        break;
      }
      if (b == 0) {
        a--;
      }
    }
    if (b == 0 && test[h] < aa[a][0]) {
      test[h] = aa[a][0];
      b = aa[a][1] - 1;
      if (b == 0) {
        a--;
      }
    }
  }
  for (int h = 0; h < n; h++) {
    ans += test[h];
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define int long long
#define mod (int)(1000000007)
using namespace std;
int test[213456];

signed main() {
  int n, k, b = 0, ans = 0;
  cin >> n >> k;
  int a = k - 1;
  for (int h = 0; h < n; h++) {
    cin >> test[h];
  }
  sort(test, test + n);
  vector<vector<long long>> aa(k, vector<long long>(2));
  for (int h = 0; h < k; h++) {
    cin >> aa[h][1] >> aa[h][0];
  }
  sort(aa.begin(), aa.end());
  for (int h = 0; h < n; h++) {
    if (b != 0 && test[h] < aa[a][0]) {
      test[h] = aa[a][0];
      b--;
      if (a == 0 && b == 0) {
        break;
      }
      if (b == 0) {
        a--;
      }
    }
    if (b == 0 && test[h] < aa[a][0]) {
      test[h] = aa[a][0];
      b = aa[a][1] - 1;
      if (a == 0 && b == 0) {
        break;
      }
      if (b == 0) {
        a--;
      }
    }
  }
  for (int h = 0; h < n; h++) {
    ans += test[h];
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a.begin(), a.end());
  vector<ii> b(m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &b[i].second, &b[i].first);
  }
  sort(b.begin(), b.end(), greater<ii>());
  vector<int> c(n);
  for (int i = 0, k = 0; i < m && k < n; ++i) {
    int len = min(n - i, b[i].second);
    while (len--)
      c[k++] = b[i].first;
  }
  int64 ret = 0;
  for (int i = 0; i < n; ++i) {
    ret += max(a[i], c[i]);
  }
  printf("%lld\n", ret);
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a.begin(), a.end());
  vector<ii> b(m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &b[i].second, &b[i].first);
  }
  sort(b.begin(), b.end(), greater<ii>());
  vector<int> c(n);
  for (int i = 0, k = 0; i < m && k < n; ++i) {
    int len = min(n - k, b[i].second);
    while (len--)
      c[k++] = b[i].first;
  }
  int64 ret = 0;
  for (int i = 0; i < n; ++i) {
    ret += max(a[i], c[i]);
  }
  printf("%lld\n", ret);
  return 0;
}