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

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vii;
typedef vector<char> vc;
typedef vector<vc> vcc;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define in insert
#define sz(v) v.size()
#define mina(a, b) (a) = min((a), (b));
#define maxa(a, b) (a) = max((a), (b));
const int INF = 1e9 + 5;

void solve() {
  int n, m;
  cin >> n >> m;
  vll v(n);
  vii oper(m);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    oper[i] = {(ll)b, a};
  }
  sort(all(v));
  sort(rall(oper));
  int cur = 0;
  for (int i = 0; i < m; i++) {
    // cout<<oper[i].S;
    int curr = cur;
    for (int j = curr; j < curr + oper[i].S; j++) {
      // cout<<"/"<<j<<"/"<<endl;
      if (v[j] < oper[i].F) {
        // cout<<v[j]<<"->"<<oper[i].F<<endl;
        v[j] = oper[i].F;
        cur++;
      } else
        break;
    }
  }
  ll sum = 0;
  for (ll i : v)
    sum += i;
  cout << sum;
}
int main() {
  int T = 1;
  // cin>>T;
  for (int i = 0; i < T; i++)
    solve();
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vii;
typedef vector<char> vc;
typedef vector<vc> vcc;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define in insert
#define sz(v) v.size()
#define mina(a, b) (a) = min((a), (b));
#define maxa(a, b) (a) = max((a), (b));
const int INF = 1e9 + 5;

void solve() {
  int n, m;
  cin >> n >> m;
  vll v(n);
  vii oper(m);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    oper[i] = {(ll)b, a};
  }
  sort(all(v));
  sort(rall(oper));
  int cur = 0;
  for (int i = 0; i < m; i++) {
    // cout<<oper[i].S;
    int curr = cur;
    for (int j = curr; j < min(curr + oper[i].S, n); j++) {
      // cout<<"/"<<j<<"/"<<endl;
      if (v[j] < oper[i].F) {
        // cout<<v[j]<<"->"<<oper[i].F<<endl;
        v[j] = oper[i].F;
        cur++;
      } else
        break;
    }
  }
  ll sum = 0;
  for (ll i : v)
    sum += i;
  cout << sum;
}
int main() {
  int T = 1;
  // cin>>T;
  for (int i = 0; i < T; i++)
    solve();
}
Problem p03038 (C++)

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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 2e5 + 10;
const int MOD = 1e9 + 7;

int32_t main() {
  boost;

  // code
  int n, m, x, y;
  cin >> n >> m;
  int a[n];
  vector<pair<int, int>> b;
  multiset<int> st;

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

  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    b.push_back({y, x});
  }

  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  int s = 0;
  for (int i = 0; i < m; ++i) {
    auto idx = st.lower_bound(b[i].first);
    if (idx != st.begin()) {
      while (*st.begin() < b[i].first and (b[i].second)--) {
        st.erase(st.begin());
        s += b[i].first;
      }
    } else {
      break;
    }
  }
  for (auto i : st) {
    s += i;
  }
  cout << s;
  return 0;
}


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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 2e5 + 10;
const int MOD = 1e9 + 7;

int32_t main() {
  boost;

  // code
  int n, m, x, y;
  cin >> n >> m;
  int a[n];
  vector<pair<int, int>> b;
  multiset<int> st;

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

  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    b.push_back({y, x});
  }

  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  int s = 0;
  for (int i = 0; i < m; ++i) {
    auto idx = st.lower_bound(b[i].first);
    if (idx != st.begin()) {
      while (*st.begin() < b[i].first and (b[i].second)-- and !st.empty()) {
        st.erase(st.begin());
        s += b[i].first;
      }
    } else {
      break;
    }
  }
  for (auto i : st) {
    s += i;
  }
  cout << s;
  return 0;
}
Problem p03038 (C++)

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

int main() {
  long long n, m, i, j, l, r, zero = 0, ans = 0, cur = 0;
  cin >> n >> m;
  vector<long long> a(n + 10);
  vector<pair<long long, long long>> b(m + 10);

  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  for (i = 0; i < n; i++) {
    a[i] = a[i + 10];
  }
  for (i = 0; i < m; i++) {
    cin >> b[i].second >> b[i].first;
  }
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  i = 0;
  while (cur < n && i < n) {
    b[i].second = min(b[i].second, n - cur);
    for (j = 0; j < b[i].second; j++) {
      a[cur] = max(a[cur], b[i].first);
      cur++;
    }
    i++;
  }
  for (i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans;
  return 0;
}


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

int main() {
  long long n, m, i, j, l, r, zero = 0, ans = 0, cur = 0;
  cin >> n >> m;
  vector<long long> a(n + 10);
  vector<pair<long long, long long>> b(m + 10);

  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  for (i = 0; i < n; i++) {
    a[i] = a[i + 10];
  }
  for (i = 0; i < m; i++) {
    cin >> b[i].second >> b[i].first;
  }
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  i = 0;
  while (cur < n && i < m) {
    b[i].second = min(b[i].second, n - cur);
    for (j = 0; j < b[i].second; j++) {
      a[cur] = max(a[cur], b[i].first);
      cur++;
    }
    i++;
  }
  for (i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vp;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

signed main(void) {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  vp v(m);
  rep(i, m) cin >> v[i].second >> v[i].first;
  sort(all(v));
  reverse(all(v));
  int ind = 0;
  rep(i, m) {
    // cout << v[i].first << " " << v[i].second << endl;
    rep(j, v[i].second) {
      if (ind < n)
        a[ind] = max(a[ind], v[i].first), ind++;
    }
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vp;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

signed main(void) {
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));
  vp v(m);
  rep(i, m) cin >> v[i].second >> v[i].first;
  sort(all(v));
  reverse(all(v));
  int ind = 0;
  rep(i, m) {
    // cout << v[i].first << " " << v[i].second << endl;
    rep(j, v[i].second) {
      if (ind < n)
        a[ind] = max(a[ind], v[i].first), ind++;
      else {
        i = m;
        break;
      }
    }
  }
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):

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

typedef long long int ll;

int main() {

  ll n, m;
  cin >> n >> m;

  ll arr[n + 1];

  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  map<ll, ll> mp;

  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    mp[b] += a;
  }

  sort(arr, arr + n);

  for (ll i = 0; i < n; i++) {

    auto it = mp.end();
    it--;
    if (it->first > arr[i]) {
      arr[i] = it->first;
      it->second--;
      if (it->second == 0) {
        mp.erase(it->first);
      }
    }
    // cout<<arr[i]<<" ";
  } // cout<<endl;
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans += arr[i];

  cout << ans << endl;
}


Fixed submission:

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

typedef long long int ll;

int main() {

  ll n, m;
  cin >> n >> m;

  ll arr[n + 1];

  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  map<ll, ll> mp;

  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    mp[b] += a;
  }

  sort(arr, arr + n);

  for (ll i = 0; i < n; i++) {

    auto it = mp.end();
    if (mp.size() == 0)
      continue;
    it--;
    if (it->first > arr[i]) {
      arr[i] = it->first;
      it->second--;
      if (it->second == 0) {
        mp.erase(it->first);
      }
    }
    // cout<<arr[i]<<" ";
  } // cout<<endl;
  ll ans = 0;
  for (ll 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) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<pair<ll, ll>> cb(N);
  rep(i, M) {
    ll b, c;
    cin >> b >> c;
    cb[i] = {c, b};
  }
  sort(cb.rbegin(), cb.rend());
  sort(A.begin(), A.end());
  int cur = 0, used = 0;
  rep(i, N) {
    if (cb[cur].second == used) {
      used = 0;
      cur++;
    }
    if (cur < M and A[i] < cb[cur].first) {
      used++;
      A[i] = cb[cur].first;
    } else {
      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) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<pair<ll, ll>> cb(M);
  rep(i, M) {
    ll b, c;
    cin >> b >> c;
    cb[i] = {c, b};
  }
  sort(cb.rbegin(), cb.rend());
  sort(A.begin(), A.end());
  int cur = 0, used = 0;
  rep(i, N) {
    if (cb[cur].second == used) {
      used = 0;
      cur++;
    }
    if (cur < M and A[i] < cb[cur].first) {
      used++;
      A[i] = cb[cur].first;
    } else {
      break;
    }
  }

  ll ans = 0;
  rep(i, N) ans += A[i];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

/*
1, 2, 3
2, 5

5, 2, 3 = 10
2, 5, 3 = 10
*/
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  vector<pair<int, int>> CB(M);
  REP(i, 0, M) cin >> CB[i].second >> CB[i].first;

  sort(ALL(A), greater<int>());
  sort(ALL(CB), greater<pair<int, int>>());

  ll ans = 0;
  int ai = 0, cbi = 0;
  REP(i, 0, N) {
    if (A[ai] > CB[cbi].first) {
      ans += A[ai++];
    } else {
      ans += CB[cbi].first;
      CB[cbi].second--;
      if (CB[cbi].second == 0)
        cbi++;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

/*
1, 2, 3
2, 5

5, 2, 3 = 10
2, 5, 3 = 10
*/
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  vector<pair<int, int>> CB(M);
  REP(i, 0, M) cin >> CB[i].second >> CB[i].first;

  sort(ALL(A), greater<int>());
  sort(ALL(CB), greater<pair<int, int>>());

  ll ans = 0;
  int ai = 0, cbi = 0;
  REP(i, 0, N) {
    if (cbi == M || A[ai] > CB[cbi].first) {
      ans += A[ai++];
    } else {
      ans += CB[cbi].first;
      CB[cbi].second--;
      if (CB[cbi].second == 0)
        cbi++;
    }
  }

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

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

using namespace std;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<ll> pq;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    pq.push(a);
  }
  vector<pair<ll, ll>> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i].second >> b[i].first;
  }
  sort(b.rbegin(), b.rend());
  ll now = 0;
  for (ll i = 0; i < m; i++) {
    for (ll j = 0; j < b[i].second; j++) {
      pq.push(b[i].first);
      if (++now == n)
        break;
    }
  }
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    ret += pq.top();
    pq.pop();
  }
  cout << ret << endl;
}

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

using namespace std;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<ll> pq;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    pq.push(a);
  }
  vector<pair<ll, ll>> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i].second >> b[i].first;
  }
  sort(b.rbegin(), b.rend());
  ll now = 0;
  for (ll i = 0; i < m; i++) {
    for (ll j = 0; j < b[i].second; j++) {
      pq.push(b[i].first);
      if (++now == n)
        break;
    }
    if (now == n)
      break;
  }
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    ret += pq.top();
    pq.pop();
  }
  cout << ret << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
using ll = long long;
using namespace std;
ll n, m, sum = 0;
ll a[110000];
pair<ll, ll> x[110000];
int main() {
  cin >> n >> m;
  ll ps1 = 0, ps2 = m - 1; // 降順ソート出来ればps2=0
  rep(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, 0, m) cin >> x[i].second >> x[i].first;
  sort(x, x + m);
  // sort(x,x+m,greater<ll>());これでコンパイルエラー;;
  // ８行目をvector<pair>>x;にして　sort(x.rbegin(),x.rend());でセグフォ;;
  sort(a, a + n);
  bool p = true;
  while (p) {
    if (x[ps2].first > a[ps1] && x[ps2].second > 0) {
      sum += x[ps2].first - a[ps1];
      ps1++;
      x[ps2].second--;
    } else if (x[ps2].first <= a[ps1])
      p = false;
    else if (x[ps2].second == 0)
      ps2--; // 降順ソート出来たらps2++;の予定だった..
  }
  cout << sum;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
using ll = long long;
using namespace std;
ll n, m, sum = 0;
ll a[110000];
pair<ll, ll> x[110000];
int main() {
  cin >> n >> m;
  ll ps1 = 0, ps2 = m - 1; // 降順ソート出来ればps2=0
  rep(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, 0, m) cin >> x[i].second >> x[i].first;
  sort(x, x + m);
  // sort(x,x+m,greater<ll>());これでコンパイルエラー;;
  // ８行目をvector<pair>>x;にして　sort(x.rbegin(),x.rend());でセグフォ;;
  sort(a, a + n);
  bool p = true;
  while (p && ps2 >= 0 && ps1 < n) {
    if (x[ps2].first > a[ps1] && x[ps2].second > 0) {
      sum += x[ps2].first - a[ps1];
      ps1++;
      x[ps2].second--;
    } else if (x[ps2].first <= a[ps1])
      p = false;
    else if (x[ps2].second == 0)
      ps2--; // 降順ソート出来たらps2++;の予定だった..
  }
  cout << sum;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct var {
  int b, c;
  var(int x, int y) { b = x, c = y; }
};
struct cmp {
  bool operator()(var a, var b) { return a.c < b.c; }
};
priority_queue<var, vector<var>, cmp> q;
vector<int> v;
int main() {
  freopen("inp", "r", stdin);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  // cout<<v[0]<<endl;
  for (int i = 1; i <= m; ++i) {
    int b, c;
    cin >> b >> c;
    q.push(var(b, c));
  }
  long long s = 0ll;
  int b = q.top().b, c = q.top().c, i = 0;
  q.pop();
  for (; i < (int)v.size(); ++i) {
    if ((q.empty() && b == 0) || v[i] > c)
      break;
    else if (b == 0)
      b = q.top().b, c = q.top().c, q.pop();
    s = s + max(v[i], c);
    --b;
  }
  for (; i < (int)v.size(); ++i)
    s += v[i];
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct var {
  int b, c;
  var(int x, int y) { b = x, c = y; }
};
struct cmp {
  bool operator()(var a, var b) { return a.c < b.c; }
};
priority_queue<var, vector<var>, cmp> q;
vector<int> v;
int main() {
  // freopen("inp","r",stdin);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  // cout<<v[0]<<endl;
  for (int i = 1; i <= m; ++i) {
    int b, c;
    cin >> b >> c;
    q.push(var(b, c));
  }
  long long s = 0ll;
  int b = q.top().b, c = q.top().c, i = 0;
  q.pop();
  for (; i < (int)v.size(); ++i) {
    if ((q.empty() && b == 0) || v[i] > c)
      break;
    else if (b == 0)
      b = q.top().b, c = q.top().c, q.pop();
    s = s + max(v[i], c);
    --b;
  }
  for (; i < (int)v.size(); ++i)
    s += v[i];
  cout << s << endl;
  return 0;
}
Problem p03038 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int inp[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &inp[i]);
  sort(inp, inp + n);

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

  sort(op, op + m,
       [](const ii a, const ii b) -> bool { return a.first > b.first; });

  int idx = 0;
  for (int i = 0; idx < n && i < m; i++) {
    int val = op[i].first;
    for (int j = 0; j < op[i].second; j++) {
      if (inp[idx] < val) {
        inp[idx] = val;
        idx++;
      } else {
        goto done;
      }
    }
  }

done:
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += inp[i];
  printf("%lld\n", ans);

  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int inp[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &inp[i]);
  sort(inp, inp + n);

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

  sort(op, op + m,
       [](const ii a, const ii b) -> bool { return a.first > b.first; });

  int idx = 0;
  for (int i = 0; idx < n && i < m; i++) {
    int val = op[i].first;
    for (int j = 0; idx < n && j < op[i].second; j++) {
      if (inp[idx] < val) {
        inp[idx] = val;
        idx++;
      } else {
        goto done;
      }
    }
  }

done:
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += inp[i];
  printf("%lld\n", ans);

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * written by Yaqi Zhang (zhang623@wisc.edu)
 * University of Wisconsin-Madison
 * 2020
 */
#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  vector<pair<int, int>> cbs(m);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    cbs[i] = {c, b};
  }
  sort(A.begin(), A.end());
  sort(cbs.rbegin(), cbs.rend());
  int idx = 0;
  long long ans = accumulate(A.begin(), A.end(), 0LL);
  bool done = false;
  for (const auto &cb : cbs) {
    int c = cb.first, b = cb.second;
    // maximum is b
    for (int i = 0; i < b; ++i) {
      if (A[idx] < c)
        ans += c - A[idx++];
      if (idx == n) {
        done = true;
        break;
      }
    }
    if (done)
      break;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*
 * written by Yaqi Zhang (zhang623@wisc.edu)
 * University of Wisconsin-Madison
 * 2020
 */
#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  vector<pair<int, int>> cbs(m);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    cbs[i] = {c, b};
  }
  sort(A.begin(), A.end());
  sort(cbs.rbegin(), cbs.rend());
  int idx = 0;
  long long ans = accumulate(A.begin(), A.end(), 0LL);
  bool done = false;
  for (const auto &cb : cbs) {
    int c = cb.first, b = cb.second;
    // maximum is b
    for (int i = 0; i < b; ++i) {
      if (A[idx] < c)
        ans += c - A[idx++];
      else {
        done = true;
        break;
      }
      if (idx == n) {
        done = true;
        break;
      }
    }
    if (done)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> TUP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000000007;

ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int main() {
  ll N, M;
  cin >> N >> M;
  ll ans = 0;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());
  vector<P> p(N);
  rep(i, M) {
    ll B, C;
    cin >> B >> C;
    p[i] = mp(C, B);
  }
  sort(p.begin(), p.end(), greater<P>());

  ll i = 0, n = 0;
  while (i < M) {
    for (int j = 1; j <= p[i].second; j++) {
      if (p[i].first > A[n])
        A[n++] = p[i].first;
      else {
        rep(k, N) { ans += A[k]; }
        cout << ans << endl;
        return 0;
      }
    }
    i++;
  }
  rep(i, N) { ans += A[i]; }
  cout << ans << endl;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> TUP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000000007;

ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int main() {
  ll N, M;
  cin >> N >> M;
  ll ans = 0;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());
  vector<P> p(M);
  rep(i, M) {
    ll B, C;
    cin >> B >> C;
    p[i] = mp(C, B);
  }
  sort(p.begin(), p.end(), greater<P>());

  ll i = 0, n = 0;
  while (i < M) {
    for (int j = 1; j <= p[i].second; j++) {
      if (p[i].first > A[n])
        A[n++] = p[i].first;
      else {
        rep(k, N) { ans += A[k]; }
        cout << ans << endl;
        return 0;
      }
    }
    i++;
  }
  rep(i, N) { ans += A[i]; }
  cout << ans << endl;
}
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;
  priority_queue<ll, vector<ll>, greater<ll>> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push(x);
  }
  vector<pair<ll, ll>> bc(m);
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    bc.at(i) = make_pair(c, b);
  }
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < bc.at(i).second; j++) {
      int mi = a.top();
      if (mi > bc.at(i).first)
        break;
      a.pop();
      a.push(bc.at(i).first);
    }
  }
  ll ans = 0;
  while (!a.empty()) {
    ans += a.top();
    a.pop();
  }
  cout << ans << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push(x);
  }
  vector<pair<ll, ll>> bc(m);
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    bc.at(i) = make_pair(c, b);
  }
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < bc.at(i).second; j++) {
      int mi = a.top();
      if (mi >= bc.at(i).first)
        break;
      a.pop();
      a.push(bc.at(i).first);
    }
  }
  ll ans = 0;
  while (!a.empty()) {
    ans += a.top();
    a.pop();
  }
  cout << ans << endl;
}
Problem p03038 (C++)

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

using namespace std;

int main() {
  int i, j;
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> P(M);
  for (i = 0; i < N; i++)
    cin >> A[i];
  // 都合上，BとCを逆順に入力している
  for (i = 0; i < M; i++)
    cin >> P[i].second >> P[i].first;

  // Aを降順にソート．Pをfirstでソート
  sort(A.begin(), A.end());
  sort(P.begin(), P.end(), greater<pair<int, int>>());

  long long int sum = 0;

  // Pを追っていく添字変数j
  j = 0;

  for (i = 0; i < N; i++) {
    // 選んだカードの方が大きければそっちを足す
    if (A[i] < P[j].first) {
      sum += P[j].first;
      // 選べる回数を一回減らす．B[j]--と同値
      P[j].second--;
      // 選べる上限に達したら次の操作に移る
      if (P[j].second == 0)
        j++;
    }
    // 選べるカードより元の数字の方が大きい
    else
      sum += A[i];
  }

  cout << sum << endl;

  return 0;
}


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

using namespace std;

int main() {
  int i, j;
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<pair<int, int>> P(M + 1);
  for (i = 0; i < N; i++)
    cin >> A[i];
  // 都合上，BとCを逆順に入力している
  for (i = 0; i < M; i++)
    cin >> P[i].second >> P[i].first;

  // Aを降順にソート．Pをfirstでソート
  sort(A.begin(), A.end());
  sort(P.begin(), P.end(), greater<pair<int, int>>());

  long long int sum = 0;

  // Pを追っていく添字変数j
  j = 0;

  for (i = 0; i < N; i++) {
    // 選んだカードの方が大きければそっちを足す
    if (A[i] < P[j].first) {
      sum += P[j].first;
      // 選べる回数を一回減らす．B[j]--と同値
      P[j].second--;
      // 選べる上限に達したら次の操作に移る
      if (P[j].second == 0)
        j++;
    }
    // 選べるカードより元の数字の方が大きい
    else
      sum += A[i];
  }

  cout << sum << endl;

  return 0;
}
Problem p03038 (C++)

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

using namespace std;
using LL = long long;
using P = pair<int, int>;

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

#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e19;
const int MOD = 1e9 + 7;

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

  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m), c(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i] >> c[i];

  vector<P> p;
  REP(i, m) { p.pb(make_pair(c[i], b[i])); }

  sort(all(a));
  sort(all(p), greater<P>());

  vector<int> d(n);
  for (int i = 0, j = 0; i < m && j < n; i++) {
    while (p[i].second > 0) {
      d[j] = p[i].first;
      j++;
      p[i].second--;
    }
  }
  priority_queue<LL> que;
  REP(i, n) que.push(a[i]);
  REP(i, n) que.push(d[i]);

  LL ans = 0;
  REP(i, n) {
    ans += que.top();
    que.pop();
  }

  cout << ans << endl;
}

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

using namespace std;
using LL = long long;
using P = pair<int, int>;

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

#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e19;
const int MOD = 1e9 + 7;

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

  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m), c(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i] >> c[i];

  vector<P> p;
  REP(i, m) { p.pb(make_pair(c[i], b[i])); }

  sort(all(a));
  sort(all(p), greater<P>());

  vector<int> d(n);
  for (int i = 0, j = 0; i < m && j < n; i++) {
    while (p[i].second > 0 && j < n) {
      d[j] = p[i].first;
      j++;
      p[i].second--;
    }
  }
  priority_queue<LL> que;
  REP(i, n) que.push(a[i]);
  REP(i, n) que.push(d[i]);

  LL ans = 0;
  REP(i, n) {
    ans += que.top();
    que.pop();
  }

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

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

struct Bc {
  int b, c;

  Bc(int inputB, int inputC) {
    b = inputB;
    c = inputC;
  }

  bool operator<(const Bc &another) const { return c < another.c; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<Bc> bc;
  int tmp_b, tmp_c;
  for (int i = 0; i < M; i++) {
    cin >> tmp_b >> tmp_c;
    bc.push_back(Bc(tmp_b, tmp_c));
  }

  sort(A.begin(), A.end());
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  int point = 0;
  bool brk = false;
  for (auto itr : bc) {
    for (int i = 0; i < itr.b; i++) {
      if (A.at(point) < itr.c) {
        A.at(point) = itr.c;
        point++;
      } else {
        brk = true;
        break;
      }
    }
    if (brk) {
      break;
    }
  }

  int64_t sum = 0;
  for (int i = 0; i < A.size(); i++) {
    sum += A.at(i);
  }
  cout << sum << endl;
}

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

struct Bc {
  int b, c;

  Bc(int inputB, int inputC) {
    b = inputB;
    c = inputC;
  }

  bool operator<(const Bc &another) const { return c < another.c; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<Bc> bc;
  int tmp_b, tmp_c;
  for (int i = 0; i < M; i++) {
    cin >> tmp_b >> tmp_c;
    bc.push_back(Bc(tmp_b, tmp_c));
  }

  sort(A.begin(), A.end());
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  int point = 0;
  bool brk = false;
  for (auto itr : bc) {
    for (int i = 0; i < itr.b; i++) {
      if (A.at(point) < itr.c) {
        A.at(point) = itr.c;
        point++;
        if (point >= A.size()) {
          brk = true;
          break;
        }
      } else {
        brk = true;
        break;
      }
    }
    if (brk) {
      break;
    }
  }

  int64_t sum = 0;
  for (int i = 0; i < A.size(); i++) {
    sum += A.at(i);
  }
  cout << sum << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v1;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    v1.pb(a);
  }
  sort(all(v1));
  vector<pair<ll, ll>> vp;
  vector<ll> v2;
  rep(i, 0, m) {
    ll b, c;
    cin >> b >> c;
    vp.pb(mp(c, b));
  }
  sort(all(vp));
  reverse(all(vp));
  rep(i, 0, vp.sz) {
    ll b = vp[i].se;
    ll c = vp[i].fi;
    rep(j, 0, b) {
      if (v2.sz > 100100)
        break;
      v2.pb(c);
    }
  }
  sort(all(v2));
  reverse(all(v2));
  ll ans = 0;
  rep(i, 0, n) { ans += max(v1[i], v2[i]); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> v1;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    v1.pb(a);
  }
  sort(all(v1));
  vector<pair<ll, ll>> vp;
  vector<ll> v2;
  rep(i, 0, m) {
    ll b, c;
    cin >> b >> c;
    vp.pb(mp(c, b));
  }
  sort(all(vp));
  reverse(all(vp));
  rep(i, 0, vp.sz) {
    ll b = vp[i].se;
    ll c = vp[i].fi;
    rep(j, 0, b) {
      if (v2.sz > 100100)
        break;
      v2.pb(c);
    }
  }
  sort(all(v2));
  reverse(all(v2));
  rep(i, 0, 100100) { v2.pb(0); }
  ll ans = 0;
  rep(i, 0, n) { ans += max(v1[i], v2[i]); }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<pair<ll, int>> bc(m);
  rep(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    bc[i] = {c, b};
  }
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  int l = 0;
  int i = 0;
  ll ans = 0;
  while (1) {
    if (l == n || i == n)
      break;
    int idx =
        lower_bound(a.begin() + l, a.end(), bc[i].first) - (a.begin() + l);
    idx = min(bc[i].second, idx);
    ans += bc[i].first * (ll)idx;
    l += idx;
    ++i;
  }
  for (int i = l; i < n; ++i) {
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<pair<ll, int>> bc(m);
  rep(i, m) {
    int b;
    ll c;
    cin >> b >> c;
    bc[i] = {c, b};
  }
  sort(bc.begin(), bc.end());
  reverse(bc.begin(), bc.end());
  int l = 0;
  int i = 0;
  ll ans = 0;
  while (1) {
    if (l == n || i == m)
      break;
    int idx =
        lower_bound(a.begin() + l, a.end(), bc[i].first) - (a.begin() + l);
    idx = min(bc[i].second, idx);
    ans += bc[i].first * (ll)idx;
    l += idx;
    ++i;
  }
  for (int i = l; 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;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main() {
  int N, M, b, c;
  cin >> N >> M;
  vector<int> a(N);
  vector<pii> v(M);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < M; i++) {
    cin >> b >> c;
    v[i] = pii(c, b);
  }
  sort(a.begin(), a.end());
  sort(v.begin(), v.end(), greater<pii>());

  int j = 0;
  for (int i = 0; i < N; i++) {
    int c = v[j].first;
    if (a[i] < c) {
      a[i] = c;
      v[j].second--;
    }
    if (v[j].second == 0)
      j++;
  }
  cout << accumulate(a.begin(), a.end(), 0LL) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main() {
  int N, M, b, c;
  cin >> N >> M;
  vector<int> a(N);
  vector<pii> v(M);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < M; i++) {
    cin >> b >> c;
    v[i] = pii(c, b);
  }
  sort(a.begin(), a.end());
  sort(v.begin(), v.end(), greater<pii>());

  int j = 0;
  for (int i = 0; i < N && j < M; i++) {
    int c = v[j].first;
    if (a[i] < c) {
      a[i] = c;
      v[j].second--;
    }
    if (v[j].second == 0)
      j++;
  }
  cout << accumulate(a.begin(), a.end(), 0LL) << '\n';
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, q, A[N];
pair<int, int> B[N];
multiset<int> S;
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  for (int i = 1; i <= q; i++)
    scanf("%d%d", &B[i].second, &B[i].first);
  sort(B + 1, B + q + 1);
  sort(A + 1, A + n + 1);
  vector<int> vec;
  for (int i = q; i; i--)
    while ((int)vec.size() < n && B[i].second)
      vec.push_back(B[i].first), B[i].second--;
  long long Mx = 0, sum = 0;
  for (int i = 0; i < n; i++)
    sum += vec[i] - A[i + 1], Mx = max(Mx, sum);
  for (int i = 1; i <= n; i++)
    Mx += A[i];
  printf("%lld\n", Mx);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, q, A[N];
pair<int, int> B[N];
multiset<int> S;
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  for (int i = 1; i <= q; i++)
    scanf("%d%d", &B[i].second, &B[i].first);
  sort(B + 1, B + q + 1);
  sort(A + 1, A + n + 1);
  vector<int> vec;
  for (int i = q; i; i--)
    while ((int)vec.size() < n && B[i].second)
      vec.push_back(B[i].first), B[i].second--;
  long long Mx = 0, sum = 0;
  for (int i = 0; i < min(n, (int)vec.size()); i++)
    sum += vec[i] - A[i + 1], Mx = max(Mx, sum);
  for (int i = 1; i <= n; i++)
    Mx += A[i];
  printf("%lld\n", Mx);
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n), b(m), c(m);
  vector<pair<ll, ll>> cmp;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, m) {
    cin >> b[i] >> c[i];
    cmp.eb(c[i], b[i]);
  }
  sort(cmp.begin(), cmp.end(), greater<pair<ll, ll>>());
  sort(a.begin(), a.end());
  int k = 0;
  rep(i, m) {
    rep(j, cmp[i].se) {
      if (a[k] <= cmp[i].fi)
        sum += cmp[i].fi - a[k];
      k++;
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n), b(m), c(m);
  vector<pair<ll, ll>> cmp;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, m) {
    cin >> b[i] >> c[i];
    cmp.eb(c[i], b[i]);
  }
  sort(cmp.begin(), cmp.end(), greater<pair<ll, ll>>());
  sort(a.begin(), a.end());
  int k = 0;
  rep(i, m) {
    rep(j, cmp[i].se) {
      if (k == n)
        break;
      if (a[k] <= cmp[i].fi)
        sum += cmp[i].fi - a[k];
      k++;
    }
  }
  cout << sum << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef long long ll;
ll inf = -1;
ll mod = pow(10, 9) + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<P> 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(), greater<P>());
  vector<int> c;
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < cb[i].second; j++) {
      if (cnt >= n) {
        break;
      }
      c.push_back(cb[i].first);
      cnt++;
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += max(a[i], c[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef long long ll;
ll inf = -1;
ll mod = pow(10, 9) + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<P> 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(), greater<P>());
  vector<int> c;
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < cb[i].second; j++) {
      if (cnt >= n) {
        break;
      }
      c.push_back(cb[i].first);
      cnt++;
    }
  }
  ll ans = 0;
  if (c.size() < n) {
    for (int i = 0; i < n - c.size(); i++) {
      c.push_back(0);
    }
  }
  for (int i = 0; i < n; i++) {
    ans += max(a[i], c[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

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

using namespace std;

typedef long long ll;
constexpr ll MOD = 1000000007LL;

bool cmp(const pair<int, ll> &a, const pair<int, ll> &b) {
  if (a.second > b.second)
    return true;
  else if (a.second == b.second && a.first > b.first)
    return true;
  else
    return false;
}

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
#ifdef DEBUG
  for (int i = 0; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
#endif
  vector<pair<int, ll>> Q(M);
  for (int j = 0; j < M; j++) {
    int b;
    ll c;
    cin >> b >> c;
    Q.push_back(make_pair(b, c));
  }
  sort(Q.begin(), Q.end(), cmp);
#ifdef DEBUG
  for (int j = 0; j < M; j++) {
    cout << Q[j].first << " " << Q[j].second << endl;
  }
#endif
  auto itr = A.begin();
  for (int j = 0; j < M && itr != A.end(); j++) {
    if (*itr >= Q[j].second)
      break;
    for (int i = 0; i < Q[j].first; itr++, i++)
      if (itr != A.end())
        if (*itr < Q[j].second)
          *itr = Q[j].second;
  }

  ll Y = accumulate(A.begin(), A.end(), 0LL);
  cout << Y << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;
constexpr ll MOD = 1000000007LL;

bool cmp(const pair<int, ll> &a, const pair<int, ll> &b) {
  if (a.second > b.second)
    return true;
  else if (a.second == b.second && a.first > b.first)
    return true;
  else
    return false;
}

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<ll> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
#ifdef DEBUG
  for (int i = 0; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
#endif
  vector<pair<int, ll>> Q(M);
  for (int j = 0; j < M; j++) {
    int b;
    ll c;
    cin >> b >> c;
    Q.push_back(make_pair(b, c));
  }
  sort(Q.begin(), Q.end(), cmp);
#ifdef DEBUG
  for (int j = 0; j < M; j++) {
    cout << Q[j].first << " " << Q[j].second << endl;
  }
#endif
  auto itr = A.begin();
  for (int j = 0; j < M && itr != A.end(); j++) {
    if (*itr >= Q[j].second)
      break;
    for (int i = 0; i < Q[j].first && itr != A.end(); itr++, i++)
      if (*itr < Q[j].second)
        *itr = Q[j].second;
  }

  ll Y = accumulate(A.begin(), A.end(), 0LL);
  cout << Y << endl;

  return 0;
}
Problem p03038 (C++)

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

ll n, m, l, ans;
ll a[100005];
priority_queue<pll> pq;

int main() {
  cin >> n >> m;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (ll b, c, i = 0; i < m; ++i) {
    cin >> b >> c;
    pq.emplace(c, b);
  }
  while (!pq.empty()) {
    pll top = pq.top();
    pq.pop();
    while (top.second-- && a[l] < top.first) {
      a[l++] = top.first;
    }
  }
  for (ll x : a)
    ans += x;
  cout << ans;
  return 0;
}


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

ll n, m, l, ans;
ll a[100005];
priority_queue<pll> pq;

int main() {
  cin >> n >> m;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (ll b, c, i = 0; i < m; ++i) {
    cin >> b >> c;
    pq.emplace(c, b);
  }
  while (!pq.empty()) {
    pll top = pq.top();
    pq.pop();
    while (l < n && top.second-- && a[l] < top.first) {
      a[l++] = top.first;
    }
  }
  for (ll x : a)
    ans += x;
  cout << ans;
  return 0;
}