Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 1000000007
#define N 150005
#define M 1000003
#define LOG 20
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n;
int v[2][N];
ll dp[2];
vector<int> op, pos;

void fsol(int w) {

  printf("%lld\n", dp[w]);

  for (int i = 1; i <= n; i++) {

    if ((i & 1) == w && v[w][(i + 1) / 2] > 0) {

      pos.pb(i);
    }
  }

  if (!sz(pos)) {

    for (int i = 1; i <= n; i++) {

      if ((i & 1) == w && v[w][(i + 1) / 2] == dp[w]) {

        pos.pb(i);

        break;
      }
    }
  }

  for (int i = n; i > pos.back(); i--)
    op.pb(i);
  for (int i = 1; i < pos[0]; i++)
    op.pb(1);

  int ld = pos[0] - 1;

  for (int i = sz(pos) - 1; i > 0; i--) {

    for (int j = 1; j <= (pos[i] - pos[i - 1] - 1) / 2; j++)
      op.pb(pos[i - 1] + 2 - ld);

    op.pb(pos[i - 1] + 1 - ld);
  }

  printf("%d\n", sz(op));

  for (int x : op)
    printf("%d\n", x);
}

ll solve(int w) {

  for (int i = 1; i <= (n + w) / 2; i++) {

    dp[w] = dp[w] + max(0, v[w][i]);
  }

  if (dp[w] == 0)
    return *max_element(v[w] + 1, v[w] + 1 + (n + w) / 2);

  return dp[w];
}

int main() {

  scanf("%d", &n);

  for (int i = 1; i <= n; i++) {

    int x;

    scanf("%d", &x);

    v[i & 1][(i + 1) / 2] = x;
  }

  if (solve(1) > solve(0))
    fsol(1);
  else
    fsol(0);
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 1000000007
#define N 150005
#define M 1000003
#define LOG 20
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n;
int v[2][N];
ll dp[2];
vector<int> op, pos;

void fsol(int w) {

  printf("%lld\n", dp[w]);

  for (int i = 1; i <= n; i++) {

    if ((i & 1) == w && v[w][(i + 1) / 2] > 0) {

      pos.pb(i);
    }
  }

  if (!sz(pos)) {

    for (int i = 1; i <= n; i++) {

      if ((i & 1) == w && v[w][(i + 1) / 2] == dp[w]) {

        pos.pb(i);

        break;
      }
    }
  }

  for (int i = n; i > pos.back(); i--)
    op.pb(i);
  for (int i = 1; i < pos[0]; i++)
    op.pb(1);

  int ld = pos[0] - 1;

  for (int i = sz(pos) - 1; i > 0; i--) {

    for (int j = 1; j <= (pos[i] - pos[i - 1] - 1) / 2; j++)
      op.pb(pos[i - 1] + 2 - ld);

    op.pb(pos[i - 1] + 1 - ld);
  }

  printf("%d\n", sz(op));

  for (int x : op)
    printf("%d\n", x);
}

ll solve(int w) {

  for (int i = 1; i <= (n + w) / 2; i++) {

    dp[w] = dp[w] + max(0, v[w][i]);
  }

  if (dp[w] == 0)
    return dp[w] = *max_element(v[w] + 1, v[w] + 1 + (n + w) / 2);

  return dp[w];
}

int main() {

  scanf("%d", &n);

  for (int i = 1; i <= n; i++) {

    int x;

    scanf("%d", &x);

    v[i & 1][(i + 1) / 2] = x;
  }

  if (solve(1) > solve(0))
    fsol(1);
  else
    fsol(0);
}