Problem p01316 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

Int dp[2][300];
int c[20];
int x[20010];

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int i, j, k;
    for (i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (i = 0; i < n; i++) {
      cin >> x[i];
    }
    memset(dp, -1, sizeof(dp));
    dp[0][128] = 0;
    int next;
    for (i = 0; i < n; i++) {
      for (j = 0; j < 256; j++) {
        if (dp[0][j] == -1)
          continue;
        for (k = 0; k < m; k++) {
          next = j + c[k];
          if (next < 0)
            next = 0;
          if (next >= 256)
            next = 255;
          if (dp[1][next] == -1 ||
              dp[1][next] > dp[0][j] + (next - x[i]) * (next - x[i]))
            dp[1][next] = dp[0][j] + (next - x[i]) * (next - x[i]);
        }
      }
      for (j = 0; j < 256; j++) {
        dp[0][j] = dp[1][j];
        dp[1][j] = -1;
      }
    }
    Int mn = -1;
    for (j = 0; j < 256; j++) {
      if (dp[0][j] != -1) {
        if (mn == -1 || mn > dp[0][j])
          mn = dp[0][j];
      }
    }
    cout << mn << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

Int dp[20010][300];
int c[20];
int x[20010];

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int i, j, k;
    for (i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (i = 0; i < n; i++) {
      cin >> x[i];
    }
    memset(dp, -1, sizeof(dp));
    dp[0][128] = 0;
    int next;
    for (i = 0; i < n; i++) {
      for (j = 0; j < 256; j++) {
        if (dp[i][j] == -1)
          continue;
        for (k = 0; k < m; k++) {
          next = j + c[k];
          if (next < 0)
            next = 0;
          if (next >= 256)
            next = 255;
          if (dp[i + 1][next] == -1 ||
              dp[i + 1][next] > dp[i][j] + (next - x[i]) * (next - x[i]))
            dp[i + 1][next] = dp[i][j] + (next - x[i]) * (next - x[i]);
        }
      }
    }
    Int mn = -1;
    for (j = 0; j < 256; j++) {
      if (dp[n][j] != -1) {
        if (mn == -1 || mn > dp[n][j])
          mn = dp[n][j];
      }
    }
    cout << mn << endl;
  }
  return 0;
}