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

#include <cstdio>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

char b[8][16];

int main() {
  REP(i, 8) scanf("%s", b[i]);

  // ÜÝÁ½            

  char mami = 'o';
  char shal = 'x';

  char ck[2] = {mami, shal};
  int turn = 0;
  int bad = 0;

  while (bad < 2) {
    int maxcnt = 0;
    int px, py;

    // printf("bad: %d\\n", bad);

    REP(y, 8) REP(x, 8) if (b[y][x] == '.') {
      int cnt = 0;
      REP(i, 3) REP(j, 3) if (i != 1 || j != 1) {
        int dy = i - 1;
        int dx = j - 1;

        int xx = x + dx;
        int yy = y + dy;

        int cc = 0;

        while (true) {
          if (xx < 0)
            break;
          if (yy < 0)
            break;
          if (xx > 7)
            break;
          if (yy > 7)
            break;

          if (b[yy][xx] == ck[turn]) {
            cnt += cc;
            break;
          } else if (b[yy][xx] == ck[(turn + 1) % 2]) {
            cc++;
          } else {
            break;
          }

          yy += dy;
          xx += dx;
        }
      }

      if (turn == 0) {
        if (cnt > maxcnt) {
          maxcnt = cnt;
          px = x;
          py = y;
        }
      } else {
        if (cnt >= maxcnt) {
          maxcnt = cnt;
          px = x;
          py = y;
        }
      }
    }

    if (maxcnt > 0) {
      bad = 0;

      b[py][px] = ck[turn];
      REP(i, 3) REP(j, 3) if (i != 1 || j != 1) {
        int dy = i - 1;
        int dx = j - 1;

        int xx = px + dx;
        int yy = py + dy;

        while (true) {
          if (xx < 0)
            break;
          if (yy < 0)
            break;
          if (xx > 7)
            break;
          if (yy > 7)
            break;

          if (b[yy][xx] == ck[turn]) {
            dx = -dx;
            dy = -dy;
            yy += dy;
            xx += dx;

            while (yy != py || xx != px) {
              b[yy][xx] = ck[turn];
              yy += dy;
              xx += dx;
            }

            break;
          } else if (b[yy][xx] == ck[(turn + 1) % 2]) {
          } else {
            break;
          }

          xx += dx;
          yy += dy;
        }
      }

    } else {
      bad++;
    }

    turn = (turn + 1) % 2;
  }

  REP(i, 8) puts(b[i]);

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#define f first
#define s second
#define mp make_pair

typedef long long ll;

using namespace std;

int n, m, l;
int k[2000];
int s[100000];
int f[100000];

ll *sbuff;
ll *mbuff;

ll *sum[2000];
ll *memo[2000];

void init() {
  int nn = n * (n + 1) / 2;
  sbuff = (ll *)malloc(sizeof(ll) * nn);
  mbuff = (ll *)malloc(sizeof(ll) * nn);

  REP(i, n) sum[i] = (ll *)&sbuff[i * (i + 1) / 2];
  REP(i, n) memo[i] = (ll *)&mbuff[i * (i + 1) / 2];
}

ll solve(int l, int r) {
  if (l < r)
    swap(l, r);
  if (memo[l][r] != -1)
    return memo[l][r];

  int next = max(l, r) + 1;
  ll ret = 0;

  if (next == n) {
    ret = sum[l][r];
  } else {
    ret = sum[next][l] + solve(next, r);
    ret = min(ret, sum[next][r] + solve(l, next));
  }

  return memo[l][r] = ret;
}

int main() {
  scanf("%d%d%d", &n, &m, &l);
  REP(i, n) {
    scanf("%d", k + i);
    k[i]--;
  }
  REP(i, m) { scanf("%d", s + i); }

  sort(k, k + n);

  init();

  for (int i = 0; i < n; i++) {
    sum[i][i] = s[k[i]];
    if (i != n - 1) {
      for (int j = k[i]; j <= k[i + 1]; j++) {
        sum[i + 1][i] += s[j];
      }
    }
  }
  for (int len = 2; len < n; len++) {
    for (int i = 0; i + len < n; i++) {
      sum[i + len][i] = sum[i + 1][i] + sum[i + len][i + 1] - s[k[i + 1]];
    }
  }

  REP(i, n) REP(j, n) if (i >= j) sum[i][j] /= l;

  // REP(i,n) { REP(j,n) printf("%3d ", (int)sum[i][j]); puts(""); }
  // REP(i,n) printf("%d (%d)\\n", k[i], s[k[i]]);

  REP(i, n) REP(j, n) if (i >= j) memo[i][j] = -1;

  ll ans = solve(0, 0);

  cout << ans << endl;

  return 0;
}