Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

bool chmax(ll &a, ll b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void check(ll res, vec<ll> A, vec<int> op) {
  for (auto &x : op) {
    assert(0 <= x && x < A.size());
    if (x == 0)
      A.erase(A.begin());
    else if (x == (int)A.size() - 1)
      A.pop_back();
    else {
      A[x] = A[x - 1] + A[x + 1];
      A.erase(A.begin() + x + 1);
      A.erase(A.begin() + x - 1);
    }
  }
  assert(A.size() == 1);
  assert(res == A[0]);
}

void print(ll ans, vec<int> op) {
  cout << ans << "\n";
  cout << op.size() << "\n";
  for (auto &x : op)
    cout << x + 1 << "\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vec<ll> A(N);
  vec<ll> sum(2);
  vvec<int> pos(2);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > 0) {
      sum[i % 2] += A[i];
      pos[i % 2].push_back(i);
    }
  }
  vec<ll> B = A;
  vec<int> op;
  if (pos[0].empty() && pos[1].empty()) {
    int p = max_element(A.begin(), A.end()) - A.begin();
    for (int i = N - 1; i > p; i--)
      op.push_back(i);
    for (int i = 0; i < p; i++)
      op.push_back(0);
    print(A[p], op);
    return 0;
  }

  vec<int> left;
  ll ans = 0;
  int sign = 0;
  if (sum[0] < sum[1]) {
    ans = sum[1];
    left = pos[1];
    sign = 1;
  } else {
    ans = sum[0];
    left = pos[0];
  }
  int n = left.size();
  int l = left[0], r = left[n - 1] + 1;
  for (int i = N - 1; i >= r; i--) {
    op.push_back(i);
    A.pop_back();
  }
  for (int i = 0; i < l; i++) {
    op.push_back(0);
    A.erase(A.begin());
    sign ^= 1;
  }
  for (auto &x : left)
    x -= l;
  bool update = true;
  while (update) {
    update = false;
    for (int i = (int)A.size() - 2; i > 0; i--)
      if (i == sign && A[i] < 0) {
        op.push_back(i);
        for (auto &x : left) {
          if (x > i + 1)
            x--;
          if (x > i - 1)
            x--;
        }
        A[i] = A[i + 1] - A[i - 1];
        A.erase(A.begin() + i + 1);
        A.erase(A.begin() + i - 1);
        update = true;
        break;
      }
  }
  update = true;
  set<int> s;
  for (auto &x : left)
    s.insert(x);
  int now = 0;
  for (int i = 1; i < A.size(); i += 2) {
    op.push_back(i - now);
    now += 2;
  }
  check(ans, B, op);
  print(ans, op);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

bool chmax(ll &a, ll b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void check(ll res, vec<ll> A, vec<int> op) {
  for (auto &x : op) {
    assert(0 <= x && x < A.size());
    if (x == 0)
      A.erase(A.begin());
    else if (x == (int)A.size() - 1)
      A.pop_back();
    else {
      A[x] = A[x - 1] + A[x + 1];
      A.erase(A.begin() + x + 1);
      A.erase(A.begin() + x - 1);
    }
  }
  assert(A.size() == 1);
  assert(res == A[0]);
}

void print(ll ans, vec<int> op) {
  cout << ans << "\n";
  cout << op.size() << "\n";
  for (auto &x : op)
    cout << x + 1 << "\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vec<ll> A(N);
  vec<ll> sum(2);
  vvec<int> pos(2);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > 0) {
      sum[i % 2] += A[i];
      pos[i % 2].push_back(i);
    }
  }
  vec<ll> B = A;
  vec<int> op;
  if (pos[0].empty() && pos[1].empty()) {
    int p = max_element(A.begin(), A.end()) - A.begin();
    for (int i = N - 1; i > p; i--)
      op.push_back(i);
    for (int i = 0; i < p; i++)
      op.push_back(0);
    print(A[p], op);
    return 0;
  }

  vec<int> left;
  ll ans = 0;
  int sign = 0;
  if (sum[0] < sum[1]) {
    ans = sum[1];
    left = pos[1];
    sign = 1;
  } else {
    ans = sum[0];
    left = pos[0];
  }
  int n = left.size();
  int l = left[0], r = left[n - 1] + 1;
  for (int i = N - 1; i >= r; i--) {
    op.push_back(i);
    A.pop_back();
  }
  for (int i = 0; i < l; i++) {
    op.push_back(0);
    A.erase(A.begin());
    sign ^= 1;
  }
  for (auto &x : left)
    x -= l;
  bool update = true;
  while (update) {
    update = false;
    for (int i = (int)A.size() - 2; i > 0; i--)
      if (i % 2 == sign && A[i] < 0) {
        op.push_back(i);
        for (auto &x : left) {
          if (x > i + 1)
            x--;
          if (x > i - 1)
            x--;
        }
        A[i] = A[i + 1] - A[i - 1];
        A.erase(A.begin() + i + 1);
        A.erase(A.begin() + i - 1);
        update = true;
        break;
      }
  }
  update = true;
  set<int> s;
  for (auto &x : left)
    s.insert(x);
  int now = 0;
  for (int i = 1; i < A.size(); i += 2) {
    op.push_back(i - now);
    now += 2;
  }
  check(ans, B, op);
  print(ans, op);
}