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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define vi vector<int>
#define vl vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define two ios_base::sync_with_stdio(0)
#define powerofn cin.tie(0)
using namespace std;

ll const MOD = 998244353;

int const max_n = 1e6 + 10;

void yes() { cout << "YES" << '\\n'; }

void no() { cout << "NO" << '\\n'; }

ll a[max_n];

void solve() {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);

  ll sum = 0;
  sum += a[1];
  n -= 2;

  int id = 2;

  while (1) {
    if (n <= 0)
      break;
    sum += a[id];
    n--;
    // cout << a[id] << ' ';
    if (n <= 0)
      break;

    sum += a[id];
    n--;
    if (n <= 0)
      break;

    // cout << a[id] << ' ';

    id++;
  }

  cout << sum;
}

int main() {
  two;
  powerofn;

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define vi vector<int>
#define vl vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define two ios_base::sync_with_stdio(0)
#define powerofn cin.tie(0)
using namespace std;

ll const MOD = 998244353;

int const max_n = 1e6 + 10;

void yes() { cout << "YES" << '\\n'; }

void no() { cout << "NO" << '\\n'; }

ll fact(int n) {
  ll ans = 1;
  for (int i = 2; i <= n; i++) {
    ans = (ans * i) % MOD;
  }

  return ans;
}

ll powM(int n, int x) {
  if (x == 0) {
    return 1;
  }
  if (x == 1) {
    return n % MOD;
  }

  if (x % 2 == 0) {
    ll a = powM(n, x / 2);
    return (a * a) % MOD;
  } else {
    return (n * powM(n, x - 1)) % MOD;
  }
}

ll C(int n, int k) {
  return (fact(n) * powM((fact(k) * fact(n - k)) % MOD, MOD - 2)) % MOD;
}

ll A(int n, int k) {
  return (fact(n) * powM(fact(n - k) % MOD, MOD - 2)) % MOD;
}

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

struct segtree {

  vl tree;
  int size;

  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    tree.assign(2 * size - 1, 0);
  }
  void build(vi &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < a.size())
        tree[x] = a[lx];
    } else {
      int m = (lx + rx) / 2;
      build(a, 2 * x + 1, lx, m);
      build(a, 2 * x + 2, m, rx);
      tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
    }
  }

  void build(vi &a) {
    init(a.size());
    build(a, 0, 0, size);
  }

  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      tree[x] = v;
      return;
    }
    int m = (lx + rx) / 2;
    if (i < m) {
      set(i, v, 2 * x + 1, lx, m);
    } else {
      set(i, v, 2 * x + 2, m, rx);
    }
    tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
  }

  void set(int i, int v) { set(i, v, 0, 0, size); }

  ll sum(int l, int r, int x, int lx, int rx) {
    if (l >= rx || lx >= r) {
      return 0;
    }
    if (lx >= l && rx <= r) {
      return tree[x];
    }
    int m = (rx + lx) / 2;
    ll s1 = sum(l, r, 2 * x + 1, lx, m);
    ll s2 = sum(l, r, 2 * x + 2, m, rx);
    return s1 + s2;
  }

  ll sum(int l, int r) { return sum(l, r, 0, 0, size); }
};

int a[3 * max_n];

void solve() {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);

  ll sum = 0;
  sum += a[1];
  n -= 2;

  int id = 2;

  while (1) {
    sum += a[id];
    n--;
    // cout << a[id] << ' ';
    if (n == 0)
      break;

    sum += a[id];
    n--;
    if (n == 0)
      break;

    // cout << a[id] << ' ';

    id++;
  }

  cout << sum;
}

int main() {
  two;
  powerofn;

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}