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

#include <algorithm>
#include <stdio.h>
#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);

using namespace std;
double b[110000];
double c[110000];
int w[110000];
double dp[110000];
int n;
double solve(int a) {
  if (a == n)
    return 0;
  if (dp[a] > -0.5)
    return dp[a];
  double ret = 999999999;
  double p = 1;
  double ks = 0;
  int val = 0;
  for (int i = 1; i <= 100; i++) {
    if (a + i > n)
      break;
    if (p < 1e-9)
      break;
    val += w[a + i - 1];
    ks += p * (1.0 - c[a + i - 1]) * val;
    p *= c[a + i - 1];
    ret = min(ret, solve(a + i) + (ks + p * val) / p + (a + i == n ? 0 : 1));
  }
  return dp[a] = ret;
}
int main() {
  BEGIN_STACK_EXTEND(60 * 1024 * 1024)
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++) {
      scanf("%lf", b + i);
    }
    int now = 0;
    for (int i = 0; i < a; i++) {
      if (i && b[i] > 0.999 && b[i - 1] > 0.999) {
        w[now - 1]++;
      } else {
        w[now] = 1;
        c[now] = b[i];
        now++;
      }
    }
    n = now;
    for (int i = 0; i <= n; i++) {
      dp[i] = -1;
    }
    printf("%.12f\\n", solve(0));
    //	for(int i=0;i<=n;i++)printf("%f ",dp[i]);
    //	printf("\\n");
  }
  END_STACK_EXTEND
}

Error: WA

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
double b[110000];
double c[110000];
int w[110000];
double dp[110000];
int n;
double solve(int a) {
  if (a == n)
    return 0;
  if (dp[a] > -0.5)
    return dp[a];
  double ret = 999999999;
  double p = 1;
  double ks = 0;
  int val = 0;
  for (int i = 1; i <= 100; i++) {
    if (a + i > n)
      break;
    if (p < 1e-9)
      break;
    val += w[a + i - 1];
    ks += p * (1.0 - c[a + i - 1]) * val;
    p *= c[a + i - 1];
    ret = min(ret, solve(a + i) + (ks + p * val) / p + (a + i == n ? 0 : 1));
  }
  return dp[a] = ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++) {
      scanf("%lf", b + i);
    }
    int now = 0;
    for (int i = 0; i < a; i++) {
      if (i && b[i] > 0.999 && b[i - 1] > 0.999) {
        w[now - 1]++;
      } else {
        w[now] = 1;
        c[now] = b[i];
        now++;
      }
    }
    n = now;
    for (int i = 0; i <= n; i++) {
      dp[i] = -1;
    }
    printf("%.12f\\n", solve(0));
    //	for(int i=0;i<=n;i++)printf("%f ",dp[i]);
    //	printf("\\n");
  }
}