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

#include <bits/stdc++.h>
#include <iomanip>
#define fast                                                                   \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define N 1000000
#define ar array
#define inf 1e18
#define mod 998244353
#define ld long double
#define pb push_back
#define f first
#define s second
typedef long long int ll;
using namespace std;
/*

ll d[N];
set<pair<ll,ll>>s;
ll par[N];
vector<pair<ll,ll>>adj[N];
void Dijkstra(ll v){
        d[v]=0;
        s.insert({d[v],v});
        par[v]=v;
        while(!s.empty()){
                ll u=s.begin()->second;
                s.erase(s.begin());
                for(auto x:adj[u]){
                        if(d[x.first]>d[u]+x.second){
                                s.erase({d[x.first],x.first});
                                d[x.first]=d[u]+x.second;
                                s.insert({d[x.first],x.first});
                                par[x.first]=u;
                        }
                }
        }
}*/
int main(void) {
  fast;
  int n, i;
  cin >> n;
  ll ans = 0;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  vector<int> v;
  if (n == 1)
    cout << 0 << endl;
  else {
    v.pb(0);
    v.pb(a[0]);
    i = 1;
    while (v.size() < n) {
      v.pb(a[i]);
      if (v.size() >= n)
        break;
      v.pb(a[i]);
      if (v.size() >= n)
        break;
      i++;
    }
    for (auto x : v)
      ans += x;
    cout << ans;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#define fast                                                                   \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define N 1000000
#define ar array
#define inf 1e18
#define mod 998244353
#define ld long double
#define pb push_back
#define f first
#define s second
typedef long long int ll;
using namespace std;
/*

ll d[N];
set<pair<ll,ll>>s;
ll par[N];
vector<pair<ll,ll>>adj[N];
void Dijkstra(ll v){
        d[v]=0;
        s.insert({d[v],v});
        par[v]=v;
        while(!s.empty()){
                ll u=s.begin()->second;
                s.erase(s.begin());
                for(auto x:adj[u]){
                        if(d[x.first]>d[u]+x.second){
                                s.erase({d[x.first],x.first});
                                d[x.first]=d[u]+x.second;
                                s.insert({d[x.first],x.first});
                                par[x.first]=u;
                        }
                }
        }
}*/
int main(void) {
  fast;
  int n, i;
  cin >> n;
  ll ans = 0;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  vector<int> v;
  v.pb(0);
  v.pb(a[0]);
  i = 1;
  while (1) {
    v.pb(a[i]);
    if (v.size() == n)
      break;
    v.pb(a[i]);
    if (v.size() == n)
      break;
    i++;
  }
  for (auto x : v)
    ans += x;
  cout << ans;
}