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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;

#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n;
  cin >> n;

  vlli a(n);
  forn(i, 0, n) cin >> a[i];
  sort(all(a), greater<ll>());

  ll ans = 0;
  forn(i, 1, n) { ans += a[i / 2]; }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;

#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll a[n], ans = 0;
  forn(i, 0, n) cin >> a[i];
  sort(a, a + n);
  ll z = n - 1;
  ans += a[n - 1];
  ll j = n - 1, x;
  z--;
  while (1) {
    j--;
    x = a[j];
    ans += x;

    z--;
    if (z == 0) {
      break;
    }
    ans += x;
    z--;
    if (z == 0) {
      break;
    }
  }
  cout << ans << endl;
}