Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

VL a, ans;
void push(int x) {
  ans.push_back(x);
  if (x == 0 || x == a.size() - 1)
    a.erase(a.begin() + x);
  else {
    a[x] = a[x - 1] + a[x + 1];
    a.erase(a.begin() + x + 1);
    a.erase(a.begin() + x - 1);
  }
}

int main() {
  int n;
  cin >> n;
  a.resize(n);
  REP(i, n) a[i] = lin();

  ll even = 0, odd = 0;
  REP(i, n) {
    if (a[i] < 0)
      continue;
    if (i % 2 == 0)
      even += a[i];
    else
      odd += a[i];
  }

  ll ma = max(even, odd);

  if (ma == 0) {
    ll m = -1e18, pos;
    REP(i, n) {
      if (a[i] > m) {
        m = a[i];
        pos = i;
      }
    }
    cout << m << endl << n - 1 << endl;
    REP(_, pos) cout << 1 << endl;
    REP(_, n - 1 - pos) cout << 2 << endl;
    assert(0);
    return 0;
  }

  cout << ma << endl;

  if (odd > even) {
    push(0);
  }

  if (a.size() % 2 == 0)
    push(a.size() - 1);
  while (a[0] < 0) {
    push(0);
    push(0);
  }
  while (a.back() < 0) {
    push(a.size() - 1);
    push(a.size() - 1);
  }
  while (1) {
    bool ok = true;
    REP(i, a.size()) {
      if (i % 2 == 0 && a[i] < 0) {
        push(i);
        ok = false;
        // for (int x : a) cout << x << " ";
        //     cout << endl;
        break;
      }
    }
    if (ok)
      break;
  }
  int hoge = a.size() / 2;
  REP(i, hoge) { push(1); }

  cout << ans.size() << endl;
  for (int x : ans)
    cout << x + 1 << endl;

  assert(a[0] == ma && a.size() == 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

VL a, ans;
void push(int x) {
  ans.push_back(x);
  if (x == 0 || x == a.size() - 1)
    a.erase(a.begin() + x);
  else {
    a[x] = a[x - 1] + a[x + 1];
    a.erase(a.begin() + x + 1);
    a.erase(a.begin() + x - 1);
  }
}

int main() {
  int n;
  cin >> n;
  a.resize(n);
  REP(i, n) a[i] = lin();

  ll even = 0, odd = 0;
  REP(i, n) {
    if (a[i] < 0)
      continue;
    if (i % 2 == 0)
      even += a[i];
    else
      odd += a[i];
  }

  ll ma = max(even, odd);

  if (ma == 0) {
    ll m = -1e18, pos;
    REP(i, n) {
      if (a[i] > m) {
        m = a[i];
        pos = i;
      }
    }
    cout << m << endl << n - 1 << endl;
    int p = n;
    REP(_, pos) {
      cout << 1 << endl;
      n--;
    }
    REP(_, p - 1 - pos) {
      cout << n << endl;
      n--;
    }
    return 0;
  }

  cout << ma << endl;

  if (odd > even) {
    push(0);
  }

  if (a.size() % 2 == 0)
    push(a.size() - 1);
  while (a[0] < 0) {
    push(0);
    push(0);
  }
  while (a.back() < 0) {
    push(a.size() - 1);
    push(a.size() - 1);
  }
  while (1) {
    bool ok = true;
    REP(i, a.size()) {
      if (i % 2 == 0 && a[i] < 0) {
        push(i);
        ok = false;
        // for (int x : a) cout << x << " ";
        //     cout << endl;
        break;
      }
    }
    if (ok)
      break;
  }
  int hoge = a.size() / 2;
  REP(i, hoge) { push(1); }

  cout << ans.size() << endl;
  for (int x : ans)
    cout << x + 1 << endl;

  assert(a[0] == ma && a.size() == 1);

  return 0;
}