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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int c[16];
int x[20000];
int n, m;

uint memo[20000][256];

uint solve(int now, int prev) {
  if (now == n)
    return 0;
  if (memo[now][prev] != 0xFFFFFFFF)
    return memo[now][prev];

  uint ret = 0xFFFFFFFF;
  REP(i, m) {
    int t = min(255, max(0, prev + c[i]));
    uint g = (uint)(t - x[now]) * (t - x[now]);
    ret = min(ret, g + solve(now + 1, t));
  }
  return memo[now][prev] = ret;
}

int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    REP(i, m) scanf("%d", c + i);
    REP(i, n) scanf("%d", x + i);
    memset(memo, 0xFF, sizeof(memo));
    printf("%u\\n", solve(0, 128));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int c[16];
int x[20000];
int n, m;

long long memo[256][20000];

long long solve(int now, int prev) {
  if (now == n)
    return 0;
  if (memo[now][prev] != -1)
    return memo[now][prev];

  long long ret = 1ll << 60;
  REP(i, m) {
    int t = min(255, max(0, prev + c[i]));

    int g = (t - x[now]) * (t - x[now]);
    ret = min(ret, g + solve(now + 1, t));
  }
  return memo[now][prev] = ret;
}

int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    REP(i, m) scanf("%d", c + i);
    REP(i, n) scanf("%d", x + i);
    memset(memo, -1, sizeof(memo));
    print((int)solve(0, 128));
  }
  return 0;
}