Problem p03413 (C++)

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

using namespace std;

using int64 = long long;
const int INF = 1 << 30;

int main() {
  int64 N;
  cin >> N;
  vector<int64> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int64 latte = 0, malta = 0;
  deque<int> choose(N);
  for (int i = 0; i < N; i++) {
    if (i & 1)
      malta += max(0LL, A[i]);
    else
      latte += max(0LL, A[i]);
  }

  if (latte == 0 && malta == 0) {
    latte = -INF, malta = -INF;
    for (int i = 0; i < N; i++) {
      if (i & 1)
        malta = max(malta, A[i]);
      else
        latte = max(latte, A[i]);
    }
    if (latte < malta) {
      cout << malta << endl;
      for (int i = 1; i < N; i += 2) {
        if (malta == A[i]) {
          choose[i] = true;
          break;
        }
      }
    } else {
      cout << latte << endl;
      for (int i = 0; i < N; i += 2) {
        if (latte == A[i]) {
          choose[i] = true;
          break;
        }
      }
    }
  } else if (latte < malta) {
    cout << malta << endl;
    for (int i = 1; i < N; i += 2) {
      if (A[i] > 0)
        choose[i] = true;
    }
  } else {
    cout << latte << endl;
    for (int i = 0; i < N; i += 2) {
      if (A[i] > 0)
        choose[i] = true;
    }
  }

  vector<int> operation;
  bool update = true;
  while (update) {
    update = false;
    for (int i = 2; i < choose.size(); i++) {
      if (!choose[i - 2] && !choose[i - 1] && !choose[i]) {
        operation.push_back(i);
        operation.erase(operation.begin() + i - 2);
        operation.erase(operation.begin() + i - 2);
        update = true;
        break;
      }
    }
  }

  while (choose.size() > 1) {
    if (!choose.front()) {
      operation.push_back(1);
      choose.pop_front();
    } else if (!choose.back()) {
      operation.push_back(choose.size());
      choose.pop_back();
    } else {
      operation.push_back(2);
      choose.pop_front();
      choose.pop_front();
    }
  }
  cout << operation.size() << endl;
  for (auto &s : operation)
    cout << s << endl;
}


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

using namespace std;

using int64 = long long;
const int INF = 1 << 30;

int main() {
  int64 N;
  cin >> N;
  vector<int64> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int64 latte = 0, malta = 0;
  deque<int> choose(N);
  for (int i = 0; i < N; i++) {
    if (i & 1)
      malta += max(0LL, A[i]);
    else
      latte += max(0LL, A[i]);
  }

  if (latte == 0 && malta == 0) {
    latte = -INF, malta = -INF;
    for (int i = 0; i < N; i++) {
      if (i & 1)
        malta = max(malta, A[i]);
      else
        latte = max(latte, A[i]);
    }
    if (latte < malta) {
      cout << malta << endl;
      for (int i = 1; i < N; i += 2) {
        if (malta == A[i]) {
          choose[i] = true;
          break;
        }
      }
    } else {
      cout << latte << endl;
      for (int i = 0; i < N; i += 2) {
        if (latte == A[i]) {
          choose[i] = true;
          break;
        }
      }
    }
  } else if (latte < malta) {
    cout << malta << endl;
    for (int i = 1; i < N; i += 2) {
      if (A[i] > 0)
        choose[i] = true;
    }
  } else {
    cout << latte << endl;
    for (int i = 0; i < N; i += 2) {
      if (A[i] > 0)
        choose[i] = true;
    }
  }

  vector<int> operation;
  bool update = true;
  while (update) {
    update = false;
    for (int i = 2; i < choose.size(); i++) {
      if (!choose[i - 2] && !choose[i - 1] && !choose[i]) {
        operation.push_back(i);
        choose.erase(choose.begin() + i - 2);
        choose.erase(choose.begin() + i - 2);
        update = true;
        break;
      }
    }
  }

  while (choose.size() > 1) {
    if (!choose.front()) {
      operation.push_back(1);
      choose.pop_front();
    } else if (!choose.back()) {
      operation.push_back(choose.size());
      choose.pop_back();
    } else {
      operation.push_back(2);
      choose.pop_front();
      choose.pop_front();
    }
  }
  cout << operation.size() << endl;
  for (auto &s : operation)
    cout << s << endl;
}