Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 1005;
const ll inf = (ll)1e15;
ll a[N];
bool mark[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  rep(i, 1, n + 1) cin >> a[i];
  ll ans = -inf;
  int ind = -1;
  rep(j, 0, 2) {
    ll sum = 0;
    int cnt = 0;
    for (int i = 1 + j; i <= n; i += 2)
      if (a[i] >= 0)
        sum += a[i], cnt++;
    if (sum > ans)
      ans = sum, ind = j;
  }
  cout << ans << endl;
  vi inds;
  for (int i = 1 + ind; i <= n; i += 2)
    if (a[i] >= 0)
      inds.pb(i), mark[i] = true;
  vi rem;
  int sz = n;
  rep(i, 1, inds[0]) rem.pb(1), sz--;
  repv(i, inds.back() + 1, n + 1) rem.pb(sz), sz--;
  vi newarr;
  rep(i, inds[0], inds.back() + 1) newarr.pb(i);
  while (sz(newarr) > 1) {
    vi tmp;
    rep(i, 0, sz(newarr)) {
      if (mark[newarr[i]])
        continue;
      if (mark[newarr[i - 1]] && mark[newarr[i + 1]]) {
        rem.pb(i + 1);
        break;
      }
      if (!mark[newarr[i - 1]] && !mark[newarr[i + 1]]) {
        rem.pb(i + 1);
        break;
      }
    }
    rep(i, 0, rem.back() - 1) tmp.pb(newarr[i]);
    rep(i, rem.back() + 1, sz(newarr)) tmp.pb(newarr[i]);
    swap(tmp, newarr);
  }
  cout << sz(rem) << endl;
  for (int x : rem)
    cout << x << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 1005;
const ll inf = (ll)1e15;
ll a[N];
bool mark[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int mx = INT_MIN, mxind = -1;
  rep(i, 1, n + 1) {
    cin >> a[i];
    if (a[i] > mx)
      mx = a[i], mxind = i;
  }
  if (mx < 0) {
    cout << mx << endl;
    cout << n - 1 << endl;
    repv(i, mxind + 1, n + 1) cout << i << endl;
    rep(i, 1, mxind) cout << 1 << endl;
    return 0;
  }
  ll ans = -inf;
  int ind = -1;
  rep(j, 0, 2) {
    ll sum = 0;
    int cnt = 0;
    for (int i = 1 + j; i <= n; i += 2)
      if (a[i] >= 0)
        sum += a[i], cnt++;
    if (sum > ans)
      ans = sum, ind = j;
  }
  cout << ans << endl;
  vi inds;
  for (int i = 1 + ind; i <= n; i += 2)
    if (a[i] >= 0)
      inds.pb(i), mark[i] = true;
  vi rem;
  int sz = n;
  rep(i, 1, inds[0]) rem.pb(1), sz--;
  repv(i, inds.back() + 1, n + 1) rem.pb(sz), sz--;
  vi newarr;
  rep(i, inds[0], inds.back() + 1) newarr.pb(i);
  while (sz(newarr) > 1) {
    vi tmp;
    rep(i, 0, sz(newarr)) {
      if (mark[newarr[i]])
        continue;
      if (mark[newarr[i - 1]] && mark[newarr[i + 1]]) {
        rem.pb(i + 1);
        break;
      }
      if (!mark[newarr[i - 1]] && !mark[newarr[i + 1]]) {
        rem.pb(i + 1);
        break;
      }
    }
    rep(i, 0, rem.back() - 1) tmp.pb(newarr[i]);
    rep(i, rem.back() + 1, sz(newarr)) tmp.pb(newarr[i]);
    swap(tmp, newarr);
  }
  cout << sz(rem) << endl;
  for (int x : rem)
    cout << x << endl;
  return 0;
}