Problem p00473 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#define INF 10000000
using namespace std;
int dp[2][100000][2] = {{{0}}};

int main() {

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 100000; j++) {
      dp[i][j][0] = dp[i][j][1] = INF;
    }
  }
  int N;
  cin >> N;
  int v[100000] = {0};
  for (int i = 0; i < N - 1; i++) {
    cin >> v[i + 1];
  }
  v[N - 1] = 0;
  for (int i = 0; i < N; i++) {

    // dp[i%2][0][1] = 0;
  }
  // i番目の場所までに、一方にj[mm]与えたときの時間 k==0なら右端は与えられている
  for (int i = 0; i < N; i++) {
    // cout<<"v = "<<v[i]<<endl;
    for (int j = 0; j < N + 1; j++) {
      for (int k = 0; k < 2; k++) {
        dp[(i + 1) % 2][j][k] = INF;
      }
    }
    dp[(i + 1) % 2][0][1] = v[i];

    for (int j = 0; j < N / 2; j++) {
      if (dp[i % 2][j][1] != INF) {
        dp[(i + 1) % 2][j + 1][0] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][1] + v[i]);
        dp[(i + 1) % 2][j][1] =
            min(dp[(i + 1) % 2][j][1], dp[i % 2][j][1] - v[i - 1] + v[i]);
      }
      if (dp[i % 2][j][0] != INF) {
        dp[(i + 1) % 2][j + 1][0] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][0] - v[i - 1] + v[i]);
        dp[(i + 1) % 2][j][1] =
            min(dp[(i + 1) % 2][j][1], dp[i % 2][j][0] + v[i]);
      }
    }
    /*
            for(int k = 0;k < 2;k++){
                cout<<"i = "<<i<<" : ";
                if(k==0)cout<<"k=0 : ";
                else cout<<"k=1 : ";
                for(int j = 0;j < N/2;j++){
                    if(j==N/2)cout<<".";

                    if(dp[(i+1)%2][j][k]!=INF)
                        printf("%4d ",dp[(i+1)%2][j][k]);
                    else cout<<"**** ";
                }
                cout<<endl;
            }
            cout<<endl;
            // */
  }
  /*
  for(int k = 0;k < 2;k++){
      for(int j = 0;j < N-1;j++){
          if(dp[0][j][k]!=INF)printf("%3d ",dp[0][j][k]);
          else cout<<"*** ";
      }
      cout<<endl;
  }
  /*for(int i = 0;i < N-1;i++){
      for(int j = 0;j < N-1;j++){
          if(dp[i][j][0]!=INF)
              printf("%3d ",dp[i][j][0]);
          else cout<<"*** ";
      }
      cout<<endl;
  }
  cout<<"----------"<<endl;
  for(int i = 0;i < N-1;i++){
      for(int j = 0;j < N-1;j++){
          if(dp[i][j][1]!=INF)
              printf("%3d ",dp[i][j][1]);
          else cout<<"*** ";
      }
      cout<<endl;
  }*/
  int ans = INF;
  ans = min(dp[0][N / 2 - 1][0], dp[0][N / 2 - 1][0]);

  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#define INF 10000000
using namespace std;
int dp[2][10000][2] = {{{0}}};

int main() {

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 10000; j++) {
      dp[i][j][0] = dp[i][j][1] = INF;
    }
  }
  int N;
  cin >> N;
  int v[10000];
  for (int i = 0; i < N - 1; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < N; i++) {
    dp[i][0][1] = 0;
  }
  // i番目の場所までに、一方にj[mm]与えたときの時間 k==0なら右端は与えられている
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - 1; j++) {
      if (dp[i % 2][j][1] != INF) {
        dp[(i + 1) % 2][j + 1][0] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][1] + v[i]);
        dp[(i + 1) % 2][j][1] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][0] - v[i - 1] + v[i]);
      }
      if (dp[i % 2][j][0] != INF) {
        dp[(i + 1) % 2][j + 1][0] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][1] - v[i - 1] + v[i]);
        dp[(i + 1) % 2][j][1] =
            min(dp[(i + 1) % 2][j + 1][0], dp[i % 2][j][0] + v[i]);
      }
    }
  }
  int ans = INF;
  ans = min(dp[0][N / 2 - 1][0], dp[0][N / 2 - 1][1]);
  /*for(int i = 0;i < N-1;i++){
      for(int j = 0;j < N-1;j++){
          if(dp[i][j][0]!=INF)
              printf("%3d ",dp[i][j][0]);
          else cout<<"*** ";
      }
      cout<<endl;
  }
  cout<<"----------"<<endl;
  for(int i = 0;i < N-1;i++){
      for(int j = 0;j < N-1;j++){
          if(dp[i][j][1]!=INF)
              printf("%3d ",dp[i][j][1]);
          else cout<<"*** ";
      }
      cout<<endl;
  }*/
  cout << ans << endl;
  return 0;
}