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

#include <iostream>
using namespace std;

enum Sauce { NOT_DETERMINED = 0, Tomato = 1, Cream = 2, Basil = 3 };
enum Seq { SAME, DIFF };

const int divisor = 10000;

class Pasta {
public:
  Pasta(int _N);
  ~Pasta();

  Sauce *sauceary;
  int schedule(int i, Sauce yesterday, Seq seq);

private:
  int N;
  int ***memo;
};

Pasta::Pasta(int _N) {
  N = _N;
  sauceary = new Sauce[N + 1];
  memo = new int **[N + 1];

  for (int i = 0; i <= N; i++)
    sauceary[i] = NOT_DETERMINED;

  for (int i = 0; i <= N; i++) {
    memo[i] = new int *[4];
    for (int j = 0; j < 4; j++) {
      memo[i][j] = new int[2];
      memo[i][j][SAME] = -1;
      memo[i][j][DIFF] = -1;
    }
  }
}

Pasta::~Pasta() {
  delete[] sauceary;
  delete[] memo;
}

int Pasta::schedule(int i, Sauce yesterday, Seq seq) {
  if (i == N + 1)
    return 1;
  if (memo[i][yesterday][seq] >= 0)
    return memo[i][yesterday][seq];

  if (seq == SAME && yesterday == sauceary[i]) {
    memo[i][yesterday][SAME] = 0;
    return 0;
  }

  if (sauceary[i] == NOT_DETERMINED) {
    int tmpmemo = 0;

    for (int si = 1; si <= 3; si++) {
      if ((Sauce)si == yesterday && seq == SAME)
        continue;
      else if ((Sauce)si == yesterday && seq == DIFF)
        tmpmemo += schedule(i + 1, (Sauce)si, SAME) % divisor;
      else
        tmpmemo += schedule(i + 1, (Sauce)si, DIFF) % divisor;
    }
    memo[i][yesterday][seq] = tmpmemo;
    return tmpmemo;
  } else {
    if (yesterday == sauceary[i])
      memo[i][yesterday][seq] = schedule(i + 1, sauceary[i], SAME) % divisor;
    else
      memo[i][yesterday][seq] = schedule(i + 1, sauceary[i], DIFF) % divisor;

    return memo[i][yesterday][seq];
  }
}

int main() {
  int N, K;
  cin >> N >> K;

  Pasta *pasta = new Pasta(N);

  for (int i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    pasta->sauceary[A] = (Sauce)B;
  }

  int ans;
  if (pasta->sauceary[1] == NOT_DETERMINED) {
    ans = pasta->schedule(2, Tomato, DIFF) + pasta->schedule(2, Cream, DIFF) +
          pasta->schedule(2, Basil, DIFF);
  } else {
    ans = pasta->schedule(2, pasta->sauceary[1], DIFF);
  }

  cout << ans % divisor << endl;

  delete pasta;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

enum Sauce { NOT_DETERMINED = 0, Tomato = 1, Cream = 2, Basil = 3 };
enum Seq { SAME, DIFF };

const int divisor = 10000;

class Pasta {
public:
  Pasta(int _N);
  ~Pasta();

  Sauce *sauceary;
  int schedule(int i, Sauce yesterday, Seq seq);

private:
  int N;
  int **memo;
};

Pasta::Pasta(int _N) {
  N = _N;
  static Sauce *sauceary = new Sauce[N + 1];
  memo = new int *[N + 1];

  for (int i = 0; i <= N; i++)
    sauceary[i] = NOT_DETERMINED;

  for (int i = 0; i <= N; i++) {
    memo[i] = new int[4];
    for (int j = 0; j < 4; j++)
      memo[i][j] = -1;
  }
}

Pasta::~Pasta() {
  delete[] sauceary;
  delete[] memo;
}

int Pasta::schedule(int i, Sauce yesterday, Seq seq) {
  if (i == N + 1)
    return 1;
  if (seq == SAME && yesterday == sauceary[i])
    return 0;

  if (sauceary[i] == NOT_DETERMINED) {
    switch (yesterday) {
    case Tomato:
      if (seq == SAME)
        return (schedule(i + 1, Cream, DIFF) + schedule(i + 1, Basil, DIFF)) %
               divisor;
      else if (seq == DIFF)
        return (schedule(i + 1, Tomato, SAME) + schedule(i + 1, Cream, DIFF) +
                schedule(i + 1, Basil, DIFF)) %
               divisor;
    case Cream:
      if (seq == SAME)
        return (schedule(i + 1, Tomato, DIFF) + schedule(i + 1, Basil, DIFF)) %
               divisor;
      else if (seq == DIFF)
        return (schedule(i + 1, Tomato, DIFF) + schedule(i + 1, Cream, SAME) +
                schedule(i + 1, Basil, DIFF)) %
               divisor;
    case Basil:
      if (seq == SAME)
        return (schedule(i + 1, Tomato, DIFF) + schedule(i + 1, Cream, DIFF)) %
               divisor;
      else if (seq == DIFF)
        return (schedule(i + 1, Tomato, DIFF) + schedule(i + 1, Cream, DIFF) +
                schedule(i + 1, Basil, SAME)) %
               divisor;
    }
  } else {
    if (yesterday == sauceary[i])
      return schedule(i + 1, sauceary[i], SAME) % divisor;
    else
      return schedule(i + 1, sauceary[i], DIFF) % divisor;
  }
}

int main() {
  int N, K;
  cin >> N >> K;

  Pasta *pasta = new Pasta(N);

  for (int i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    pasta->sauceary[A] = (Sauce)B;
  }

  int ans;
  if (pasta->sauceary[1] == NOT_DETERMINED) {
    ans = pasta->schedule(2, Tomato, DIFF) + pasta->schedule(2, Cream, DIFF) +
          pasta->schedule(2, Basil, DIFF);
  } else {
    ans = pasta->schedule(2, pasta->sauceary[1], DIFF);
  }

  cout << ans % divisor << endl;

  delete pasta;
  return 0;
}