Problem p03413 (C++)

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;

//-----------------------------------------------------------------------------

int n;
ll a[1010], odd_sum, even_sum;
bool flg = false;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 0) {
      flg = true;
      if (i & 1)
        odd_sum += a[i];
      else
        even_sum += a[i];
    }
  }
  vector<int> ans;
  if (!flg) {
    int ma_idx = 1;
    for (int i = 1; i <= n; i++) {
      if (a[ma_idx] < a[i])
        ma_idx = a[i];
    }
    for (int i = n; i > ma_idx; i--) {
      ans.pb(i);
    }
    for (int i = 1; i < ma_idx; i++) {
      ans.pb(1);
    }
    cout << a[ma_idx] << endl;
    cout << (int)ans.size() << endl;
    for (auto x : ans) {
      cout << x << endl;
    }
  } else {
    if (odd_sum > even_sum) {
      if (n % 2 == 1 && a[n] < 0) {
        ans.pb(n);
      }
      for (int i = n; i >= 1; i--) {
        if (i % 2 == 0) {
          if (a[i - 1] < 0) {
            ans.pb(i - 1);
          } else {
            ans.pb(i);
          }
        }
      }
      if (a[1] < 0)
        ans.pb(1);
    } else {
      if (n % 2 == 0 && a[n] < 0) {
        ans.pb(n);
      }
      for (int i = n; i >= 1; i--) {
        if (i % 2 == 1) {
          if (a[i - 1] < 0) {
            ans.pb(i - 1);
          } else {
            ans.pb(i);
          }
        }
      }
    }
    cout << max(odd_sum, even_sum) << endl;
    cout << (int)ans.size() << endl;
    for (auto x : ans) {
      cout << x << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;

//-----------------------------------------------------------------------------

int n;
ll a[1010], odd_sum, even_sum;
bool flg = false;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 0) {
      flg = true;
      if (i & 1)
        odd_sum += a[i];
      else
        even_sum += a[i];
    }
  }
  vector<int> ans;
  if (!flg) {
    int ma_idx = 1;
    for (int i = 1; i <= n; i++) {
      if (a[ma_idx] < a[i])
        ma_idx = i;
    }
    for (int i = n; i > ma_idx; i--) {
      ans.pb(i);
    }
    for (int i = 1; i < ma_idx; i++) {
      ans.pb(1);
    }
    cout << a[ma_idx] << endl;
    cout << (int)ans.size() << endl;
    for (auto x : ans) {
      cout << x << endl;
    }
  } else {
    if (odd_sum > even_sum) {
      if (n % 2 == 1 && a[n] < 0) {
        ans.pb(n);
      }
      for (int i = n; i >= 1; i--) {
        if (i % 2 == 0) {
          if (a[i - 1] < 0) {
            ans.pb(i - 1);
          } else {
            ans.pb(i);
          }
        }
      }
      if (a[1] < 0)
        ans.pb(1);
    } else {
      if (n % 2 == 0 && a[n] < 0) {
        ans.pb(n);
      }
      for (int i = n; i >= 1; i--) {
        if (i % 2 == 1) {
          if (a[i - 1] < 0) {
            ans.pb(i - 1);
          } else {
            ans.pb(i);
          }
        }
      }
    }
    cout << max(odd_sum, even_sum) << endl;
    cout << (int)ans.size() << endl;
    for (auto x : ans) {
      cout << x << endl;
    }
  }

  return 0;
}