Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>

using namespace std;

#define INFD numeric_limits<double>::infinity()
#define INFL (int)1e8
#define INFLL (long long)1e15
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loopll(i, n) for (ll i = 0; i < (ll)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopll1(i, n) for (ll i = 1; i <= (ll)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Looprll(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define Looprll1(i, n) for (ll i = (ll)n; i >= 1; i--)
#define rnd(d) (ll)((double)d + ((d >= 0) ? 0.5 : -0.5))
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

int main() {
  int n;
  cin >> n;
  vll a(n);
  Loop(i, n) { cin >> a[i]; }
  vll sum(2, 0);
  Loop(i, n) {
    if (a[i] > 0)
      sum[0] += a[i];
    i++;
  }
  Loop1(i, n - 1) {
    if (a[i] > 0)
      sum[1] += a[i];
    i++;
  }
  ll ans_sum;
  deque<bool> b(n, false);
  if (sum[0] > sum[1]) {
    Loop(i, n) {
      if (a[i] > 0)
        b[i] = true;
      i++;
    }
    ans_sum = sum[0];
  } else if (sum[1] > sum[0]) {
    Loop1(i, n - 1) {
      if (a[i] > 0)
        b[i] = true;
      i++;
    }
    ans_sum = sum[1];
  } else {
    int max_index = -1;
    int max_value = LLONG_MIN;
    Loop(i, n) {
      if (a[i] > max_value) {
        max_index = i;
        max_value = a[i];
      }
    }
    b[max_index] = true;
    ans_sum = max_value;
  }
  int cnt = 0;
  vector<int> ans;
  Loopr(i, n) {
    if (!b[i]) {
      ans.push_back(i);
      b.pop_back();
    } else {
      break;
    }
  }
  Loop(i, n) {
    if (!b[0]) {
      ans.push_back(0);
      b.pop_front();
    } else {
      break;
    }
  }
  if (b.size() >= 3) {
    int i = 0;
    while (i < b.size()) {
      if (i + 2 < b.size() && !b[i] && !b[i + 1] && !b[i + 2]) {
        ans.push_back(i + 1);
        b.erase(b.begin() + i + 2);
        b.erase(b.begin() + i);
      } else {
        i++;
      }
    }
    Loop(i, b.size() / 2) { ans.push_back(1); }
  }
  cout << ans_sum << endl;
  cout << ans.size() << endl;
  Loop(i, ans.size()) { cout << ans[i] + 1 << endl; }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>

using namespace std;

#define INFD numeric_limits<double>::infinity()
#define INFL (int)1e8
#define INFLL (long long)1e15
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loopll(i, n) for (ll i = 0; i < (ll)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopll1(i, n) for (ll i = 1; i <= (ll)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Looprll(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define Looprll1(i, n) for (ll i = (ll)n; i >= 1; i--)
#define rnd(d) (ll)((double)d + ((d >= 0) ? 0.5 : -0.5))
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

int main() {
  int n;
  cin >> n;
  vll a(n);
  Loop(i, n) { cin >> a[i]; }
  vll sum(2, 0);
  Loop(i, n) {
    if (a[i] > 0)
      sum[0] += a[i];
    i++;
  }
  Loop1(i, n - 1) {
    if (a[i] > 0)
      sum[1] += a[i];
    i++;
  }
  ll ans_sum;
  deque<bool> b(n, false);
  if (sum[0] > sum[1]) {
    Loop(i, n) {
      if (a[i] > 0)
        b[i] = true;
      i++;
    }
    ans_sum = sum[0];
  } else if (sum[1] > sum[0]) {
    Loop1(i, n - 1) {
      if (a[i] > 0)
        b[i] = true;
      i++;
    }
    ans_sum = sum[1];
  } else {
    int max_index = -1;
    ll max_value = LLONG_MIN;
    Loop(i, n) {
      if (a[i] > max_value) {
        max_index = i;
        max_value = a[i];
      }
    }
    b[max_index] = true;
    ans_sum = max_value;
  }
  int cnt = 0;
  vector<int> ans;
  Loopr(i, n) {
    if (!b[i]) {
      ans.push_back(i);
      b.pop_back();
    } else {
      break;
    }
  }
  Loop(i, n) {
    if (!b[0]) {
      ans.push_back(0);
      b.pop_front();
    } else {
      break;
    }
  }
  if (b.size() >= 3) {
    int i = 0;
    while (i < b.size()) {
      if (i + 2 < b.size() && !b[i] && !b[i + 1] && !b[i + 2]) {
        ans.push_back(i + 1);
        b.erase(b.begin() + i + 2);
        b.erase(b.begin() + i);
      } else {
        i++;
      }
    }
    Loop(i, b.size() / 2) { ans.push_back(1); }
  }
  cout << ans_sum << endl;
  cout << ans.size() << endl;
  Loop(i, ans.size()) { cout << ans[i] + 1 << endl; }
}