Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll from;
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll n;
vl a;
vl s;
/******* define variable end *******/
ll binary_search(ll x) {
  ll i = 0, j = n - 1;
  while (j >= i) {
    ll m = floor((i + j) / 2);
    if (a[m] == x) {
      return m;
    } else if (a[m] > x) {
      j = m - 1;
    } else {
      i = m + 1;
    }
  }
  return -1;
}

int solve() {
  cin >> n;
  RES(a, n);
  REP(i, n) { cin >> a[i]; }
  sort(ALL(a));
  ll ans = 0;
  FOR(i, 1, n) { ans += a[n - i / 2 - 1]; }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll from;
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll n;
vl a;
vl s;
/******* define variable end *******/
ll binary_search(ll x) {
  ll i = 0, j = n - 1;
  while (j >= i) {
    ll m = floor((i + j) / 2);
    if (a[m] == x) {
      return m;
    } else if (a[m] > x) {
      j = m - 1;
    } else {
      i = m + 1;
    }
  }
  return -1;
}

int solve() {
  cin >> n;
  RES(a, n);
  REP(i, n) { cin >> a[i]; }
  sort(ALL(a));
  REP(i, n) {
    if (i % 2 == 0) {
      s.push_back(a[i / 2]);
    } else {
      s.push_back(a[n - i / 2 - 1]);
    }
  }
  ll ans = 0;
  ll size = s.size();
  REP(i, n) {
    if (size == 1)
      break;
    auto ite = lower_bound(ALL(s), a[i]);
    if (ite == s.begin()) {
      ++ite;
      ans += min(*ite, s[size - 1]);
      --ite;
    } else if (ite == s.end() - 1) {
      --ite;
      ans += min(*ite, s[0]);
      ++ite;
    } else {
      ++ite;
      auto ite2 = ite;
      --ite;
      --ite;
      auto ite3 = ite;
      ans += min(*ite2, *ite3);
      ++ite;
    }
    s.erase(ite);
    size--;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}