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

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

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

ll dp[2][256];
int C[16];
int x[20000];
int N, M;

void solve() {
  rep(i, M) cin >> C[i];
  rep(i, N) cin >> x[i];
  memset(dp, -1, sizeof(dp));
  dp[0][128] = 0;
  rep(i, N) {
    int cu = i & 1;
    int ne = (i + 1) & 1;

    rep(j, 256) {
      if (dp[cu][j] == -1)
        continue;
      // cout << i << ' ' << dp[cu][j] << endl;
      rep(k, M) {
        int nx = j + C[k];
        nx = min(nx, 255);
        nx = max(nx, 0);
        int di = nx - x[i];
        if (dp[ne][nx] == -1 || dp[ne][nx] > dp[cu][j] + di * di)
          dp[ne][nx] = dp[cu][j] + di * di;
      }
      dp[cu][j] = -1;
    }
  }

  ll ans = 1LL << 50LL;
  rep(i, 256) if (dp[N & 1][i] != -1) ans = min(ans, dp[N & 1][i]);
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> N >> M && N && M)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

int n, m;
int c[16];

bool vis[256][20001];

void solve() {
  rep(i, m) cin >> c[i];
  int x[n];
  rep(i, n) cin >> x[i];
  priority_queue<pair<ll, PI>> q;
  q.push(mp(0, mp(0, 128)));
  memset(vis, 0, sizeof(vis));
  ll tans = 0;
  int y0 = 128;
  rep(i, n) {
    int mini = 0;
    int minv = 1000000000;
    rep(j, m) {
      int nv = y0 + c[j];
      nv = max(nv, 0);
      nv = min(nv, 255);
      if (minv > abs(nv - x[j])) {
        mini = j;
        minv = abs(nv - x[j]);
      }
    }
    int nv = y0 + c[mini];
    nv = max(nv, 0);
    nv = min(nv, 255);
    tans += (nv - x[i]) * (nv - x[i]);
  }
  while (!q.empty()) {
    ll cc = -q.top().F;
    int ne = q.top().S.F;
    int cv = q.top().S.S;
    q.pop();
    if (ne == n) {
      cout << cc << endl;
      return;
    }
    if (tans <= cc)
      continue;
    if (vis[cv][ne])
      continue;
    vis[cv][ne] = true;
    rep(j, m) {
      int nv = cv + c[j];
      nv = max(nv, 0);
      nv = min(nv, 255);
      if (vis[nv][ne + 1])
        continue;
      q.push(mp(-cc - (nv - x[ne]) * (nv - x[ne]), mp(ne + 1, nv)));
    }
  }
  cout << tans << endl;
}

int main(int argc, char *argv[]) {
  while (cin >> n >> m && n)
    solve();
  return 0;
}