Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define db(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;

int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);

void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }

vector<vi> g(maxn);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //	freopen("name.in","r",stdin);
  //	freopen("name.out","w",stdout);

  int n;
  cin >> n;
  vi v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(all(v));
  deque<int> d;
  d.PB(v[n - 1]);
  int ans = 0;
  for (int i = n - 2; i >= 0; i--) {
    int ele = v[i];
    int pos = -1;
    int mx = 0;
    for (int j = 0; j < d.size(); j++) {
      if (min(d[j], d[(j + 1) % d.size()]) > mx) {
        mx = min(d[j], d[(j + 1) % d.size()]);
        pos = j + 1;
      }
    }
    //	cout << mx << endl;

    ans += mx;
    if (pos <= d.size() / 2) {
      vi aux;
      for (int j = 0; j < pos; j++) {
        aux.PB(d.front());
        d.pop_front();
      }

      d.push_front(v[i]);

      while (aux.size()) {
        d.push_front(aux[aux.size() - 1]);
        aux.pop_back();
      }
    } else {
      vi aux;
      int y = d.size();
      while (y != pos - 1) {
        aux.PB(d[y]);
        d.pop_back();
        y--;
      }
      d.PB(v[i]);
      while (aux.size()) {
        d.PB(aux[aux.size() - 1]);
        aux.pop_back();
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define db(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;

int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);

void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }

vector<vi> g(maxn);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //	freopen("name.in","r",stdin);
  //	freopen("name.out","w",stdout);

  int n;
  cin >> n;
  vi v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(all(v));
  int ans = v[n - 1];
  int j = n - 2;
  int cont = 0;
  for (int i = n - 3; i >= 0; i--) {
    ans += v[j];
    cont++;
    if (cont == 2)
      cont = 0, j--;
  }
  cout << ans;
  return 0;
}