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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int32_t main() {
  // IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n;
    cin >> n;
    vector<int> v(n);
    rep(i, 0, n - 1) cin >> v[i];
    sort(all(v));
    reverse(all(v));
    int ans = 0;
    ans += v[0];
    int i = 1;
    int l = n - 1;
    l--;
    while (1) {
      if (l <= 0)
        break;
      if (l >= 2) {
        ans += v[i] * 2;
        l -= 2;
      } else {
        ans += v[i];
        l -= 1;
      }
      i++;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int32_t main() {
  // IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n;
    cin >> n;
    vector<int> v(n);
    rep(i, 0, n - 1) cin >> v[i];
    sort(all(v));
    reverse(all(v));
    int ans = 0;
    ans += v[0];
    int i = 1;
    int l = n - 1;
    l--;
    while (1) {
      if (l >= 2) {
        ans += v[i] * 2;
        l -= 2;
      } else {
        ans += v[i];
        l -= 1;
      }
      i++;
      if (l == 0)
        break;
    }
    cout << ans << endl;
  }
}