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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// 方針：DP
int min(int p, int q) {
  if (p < q)
    return p;
  return q;
}
int rss(int p, int q) { return (p - q) * (p - q); }
int limit(int i) {
  if (i > 255)
    return 255;
  if (i < 0)
    return 0;
  return i;
}
int main(void) {
  //	FILE* fp_in = freopen("data.txt", "r", stdin);

  vector<vector<int>> dp;
  vector<int> dpp;
  for (int i = 0; i < 20001; i++)
    dpp.push_back(1 << 30);
  for (int i = 0; i < 257; i++)
    dp.push_back(dpp);

  while (1) {
    int N, M;
    vector<int> c, x;
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++) {
      int tmp;
      scanf("%d", &tmp);
      c.push_back(tmp);
    }
    for (int i = 0; i < N; i++) {
      int tmp;
      scanf("%d", &tmp);
      x.push_back(tmp);
    }

    for (int i = 0; i < 256; i++)
      for (int j = 0; j < 20001; j++)
        dp[i][j] = 1 << 30;

    for (int i = 0; i < M; i++) {
      dp[limit(128 + c[i])][1] = rss(limit(128 + c[i]), x[0]);
    }

    for (int i = 1; i < N; i++)
      for (int j = 0; j < M; j++)
        for (int k = 0; k < 256; k++)
          dp[limit(k + c[j])][i + 1] =
              min(dp[k][i] + rss(limit(k + c[j]), x[i]),
                  dp[limit(k + c[j])][i + 1]);

    int ans = 1 << 30;
    for (int i = 0; i < 256; i++)
      if (dp[i][N] < ans)
        ans = dp[i][N];

    cout << ans << endl;
  }

  // while (1){}
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 方針：DP
int min(int p, int q) {
  if (p < q)
    return p;
  return q;
}
int rss(int p, int q) { return (p - q) * (p - q); }
int limit(int i) {
  if (i > 255)
    return 255;
  if (i < 0)
    return 0;
  return i;
}
int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);

  vector<vector<int>> dp;
  vector<int> dpp;
  for (int i = 0; i < 10000; i++)
    dpp.push_back(1 << 30);
  for (int i = 0; i < 256; i++)
    dp.push_back(dpp);

  while (1) {
    int N, M;
    vector<int> c, x;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++) {
      int tmp;
      cin >> tmp;
      c.push_back(tmp);
    }
    for (int i = 0; i < N; i++) {
      int tmp;
      cin >> tmp;
      x.push_back(tmp);
    }
    for (int i = 0; i < 256; i++)
      for (int j = 0; j < 10000; j++)
        dp[i][j] = 1 << 30;

    for (int i = 0; i < 256; i++)
      dp[i][0] = 0;
    for (int i = 0; i < M; i++) {
      dp[limit(128 + c[i])][1] = rss(limit(128 + c[i]), x[0]);
    }
    for (int i = 2; i <= N; i++) {
      for (int j = 0; j < M; j++) {
        for (int k = 0; k < 256; k++) {
          dp[k][i] =
              min(dp[limit(k - c[j])][i - 1] + rss(k, x[i - 1]), dp[k][i]);
          if (k == 255 && c[j] > 0)
            dp[k][i] = min(dp[limit(k)][i - 1] + rss(k, x[i - 1]), dp[k][i]);
          if (k == 0 && c[j] < 0)
            dp[k][i] = min(dp[limit(k)][i - 1] + rss(k, x[i - 1]), dp[k][i]);
        }
      }
    }
    int ans = 1 << 30;
    for (int i = 0; i < 256; i++) {
      if (dp[i][N] < ans)
        ans = dp[i][N];
    }

    cout << ans << endl;
  }

  // while (1){}
  return 0;
}