Problem p03038 (C++)

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

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define shosu(X) fixed << setprecision(X)
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; }

ll int data[100000];

int getindex(ll int *data, int lessthanthis, int l, int r) {
  if (l == r || l + 1 == r) {
    if (data[r] < lessthanthis)
      return r;
    else if (data[l] < lessthanthis)
      return l;
    else
      return -1;
  }
  int p = (l + r) / 2;
  // cout << " l :" << l << " r: " << r << " p: " << p << endl;
  if (data[p] < lessthanthis)
    return getindex(data, lessthanthis, p, r);
  else
    return getindex(data, lessthanthis, l, p);
}

// int main(){
//   REP(i, 10) data[i] = i;
//   REP(i, 10) cout << data[i] << " ";
//   cout << endl;
//   cout << getindex(data, 19, 0, 9) << endl;
// }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll int ans = 0;
  int n, m;
  cin >> n >> m;
  REP(i, n) cin >> data[i];
  sort(data, data + n);
  vector<pair<ll int, int>> vp;
  ll int maxc = -1;
  REP(i, m) {
    int b;
    ll int c;
    cin >> b >> c;
    maxc = max(maxc, c);
    c = -1 * c;
    vp.push_back(mp(c, b));
  }
  sort(All(vp));
  int target_made = getindex(data, maxc, 0, n - 1);
  // cout << "target_made : " << target_made << endl;
  int cnt = n;
  FOR(i, target_made + 1, n) {
    ans += data[i];
    cnt--;
  }
  int target_l = 0;
  int rewrite_numsum = 0;
  REP(i, vp.size()) {
    ll int c = -1 * vp[i].first;
    int b = vp[i].second;
    // cout << " target_l : " << target_l << " c : " << c << endl;
    // cout << " getindex : " << getindex(data, c, target_l, n-1) << endl;
    ll int rewrite_num =
        min(b, getindex(data, c, target_l, n - 1) - target_l + 1);
    // cout << " rewrite_num : " << rewrite_num << " rewrite_to : " << c <<
    // endl;
    if (rewrite_num < 0)
      continue;
    target_l += rewrite_num;
    ans += rewrite_num * c;
    cnt -= rewrite_num;
    rewrite_numsum += rewrite_num;
  }

  // cout << " nokori cnt: " << cnt << endl;
  for (int i = rewrite_numsum; i < rewrite_numsum + cnt; i++) {
    ans += data[i];
  }
  cout << ans << endl;
}

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

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define shosu(X) fixed << setprecision(X)
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; }

ll int data[100000];

int getindex(ll int *data, int lessthanthis, int l, int r) {
  if (l > r)
    return -1;
  if (l == r || l + 1 == r) {
    if (data[r] < lessthanthis)
      return r;
    else if (data[l] < lessthanthis)
      return l;
    else
      return -1;
  }
  int p = (l + r) / 2;
  // cout << " l :" << l << " r: " << r << " p: " << p << endl;
  if (data[p] < lessthanthis)
    return getindex(data, lessthanthis, p, r);
  else
    return getindex(data, lessthanthis, l, p);
}

// int main(){
//   REP(i, 10) data[i] = i;
//   REP(i, 10) cout << data[i] << " ";
//   cout << endl;
//   cout << getindex(data, 19, 0, 9) << endl;
// }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll int ans = 0;
  int n, m;
  cin >> n >> m;
  REP(i, n) cin >> data[i];
  sort(data, data + n);
  vector<pair<ll int, int>> vp;
  ll int maxc = -1;
  REP(i, m) {
    int b;
    ll int c;
    cin >> b >> c;
    maxc = max(maxc, c);
    c = -1 * c;
    vp.push_back(mp(c, b));
  }
  sort(All(vp));
  int target_made = getindex(data, maxc, 0, n - 1);
  // cout << "target_made : " << target_made << endl;
  int cnt = n;
  FOR(i, target_made + 1, n) {
    ans += data[i];
    cnt--;
  }
  int target_l = 0;
  int rewrite_numsum = 0;
  REP(i, vp.size()) {
    ll int c = -1 * vp[i].first;
    int b = vp[i].second;
    // cout << " target_l : " << target_l << " c : " << c << endl;
    // cout << " getindex : " << getindex(data, c, target_l, n-1) << endl;
    ll int rewrite_num =
        min(b, getindex(data, c, target_l, n - 1) - target_l + 1);
    // cout << " rewrite_num : " << rewrite_num << " rewrite_to : " << c <<
    // endl;
    if (rewrite_num < 0)
      continue;
    target_l += rewrite_num;
    ans += rewrite_num * c;
    cnt -= rewrite_num;
    rewrite_numsum += rewrite_num;
  }

  // cout << " nokori cnt: " << cnt << endl;
  for (int i = rewrite_numsum; i < rewrite_numsum + cnt; i++) {
    ans += data[i];
  }
  cout << ans << endl;
}