Problem p03566 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}

using ll = long long;
const ll mod = LLONG_MAX;

int N;
int t[110], v[110];
int maxV[40010];
// x,y方向に二倍
double dp[240][40040];
int endTime = 0;
int tempEndTime = 0;
double ans;
int main() {
  cin >> N;

  rep(i, 40010) rep(j, 240) dp[i][j] = -1;
  rep(i, N) {
    cin >> t[i];
    t[i] *= 2;
    endTime += t[i];
  }
  rep(i, N) {
    cin >> v[i];
    v[i] *= 2;
  }
  for (int i = 0; i < 40010; i++) {
    int temp = 0;
    for (int j = 0; j < N; j++) {
      temp += t[j];
      if (temp >= i) {
        maxV[i] = v[j];
        break;
      }
    }
  }

  // //speedが増える時
  // (speed+(speed+1))*1/2
  // //speedが減る時
  // (speed+(speed-1)*1)/2
  // //speedが変わらないとき
  // (speed+speed)*1/2;
  dp[0][0] = 0;
  for (int Time = 1; Time <= endTime; Time++) {
    for (int speed = 0; speed <= 220; speed++) {
      if (dp[speed][Time - 1] < 0)
        continue;
      if (speed > maxV[Time])
        continue;
      if (speed < maxV[Time] && speed != 0) {
        // 等速か減速か加速
        dp[speed + 1][Time] =
            max(dp[speed + 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed + 1)) / 2.0);
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
        dp[speed - 1][Time] =
            max(dp[speed - 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed - 1)) / 2.0);
      } else if (speed < maxV[Time]) {
        // 等速か加速
        dp[speed + 1][Time] =
            max(dp[speed + 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed + 1)) / 2.0);
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
      } else {
        // 等速か減速
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
        dp[speed - 1][Time] =
            max(dp[speed - 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed - 1)) / 2.0);
      }
    }
  }

  cout << fixed << setprecision(20) << dp[0][endTime] / 4.0 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}

using ll = long long;
const ll mod = LLONG_MAX;

int N;
int t[110], v[110];
int maxV[40010];
// x,y方向に二倍
double dp[240][40040];
int endTime = 0;
int tempEndTime = 0;
double ans;
int main() {
  cin >> N;

  rep(i, 40010) rep(j, 240) dp[j][i] = -1;
  rep(i, N) {
    cin >> t[i];
    t[i] *= 2;
    endTime += t[i];
  }
  rep(i, N) {
    cin >> v[i];
    v[i] *= 2;
  }
  for (int i = 0; i < 40010; i++) {
    int temp = 0;
    for (int j = 0; j < N; j++) {
      temp += t[j];
      if (temp >= i) {
        maxV[i] = v[j];
        break;
      }
    }
  }

  // //speedが増える時
  // (speed+(speed+1))*1/2
  // //speedが減る時
  // (speed+(speed-1)*1)/2
  // //speedが変わらないとき
  // (speed+speed)*1/2;
  dp[0][0] = 0;
  for (int Time = 1; Time <= endTime; Time++) {
    for (int speed = 0; speed <= 220; speed++) {
      if (dp[speed][Time - 1] < 0)
        continue;
      if (speed > maxV[Time])
        continue;
      if (speed < maxV[Time] && speed != 0) {
        // 等速か減速か加速
        dp[speed + 1][Time] =
            max(dp[speed + 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed + 1)) / 2.0);
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
        dp[speed - 1][Time] =
            max(dp[speed - 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed - 1)) / 2.0);
      } else if (speed < maxV[Time]) {
        // 等速か加速
        dp[speed + 1][Time] =
            max(dp[speed + 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed + 1)) / 2.0);
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
      } else {
        // 等速か減速
        dp[speed][Time] =
            max(dp[speed][Time],
                dp[speed][Time - 1] + (double)(speed + (speed)) / 2.0);
        dp[speed - 1][Time] =
            max(dp[speed - 1][Time],
                dp[speed][Time - 1] + (double)(speed + (speed - 1)) / 2.0);
      }
    }
  }

  cout << fixed << setprecision(20) << dp[0][endTime] / 4.0 << endl;
}