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

#include <bits/stdc++.h>
using namespace std;

int foo(int huga) {
  if (huga <= 0)
    return 0;
  else if (huga >= 255)
    return 255;
  return huga;
}

int main() {
  int N, M, C[16], x, y = 128, i, j, k, l, mi, z;
  while (1) {
    int temp[20003][256] = {}, temp3[3][256] = {}, count0 = 0, count1 = 0,
        count2 = 0;
    temp[0][128] = 0;
    for (i = 0; i < 3; i++)
      for (j = 0; j < 256; j++)
        temp3[i][j] = -1;
    temp3[0][count0++] = 128;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (i = 1; i <= N; i++) {
      cin >> x;
      for (j = 0; j < 256; j++)
        temp[i][j] = -1;
      for (j = 0; j < count0; j++) {
        for (k = 0; k < M; k++) {
          z = foo(C[k] + temp3[0][j]);
          if (temp[i][z] == -1) {
            temp[i][z] = temp[i - 1][temp3[0][j]] + (x - z) * (x - z);
            temp3[1][count1] = z;
            count1++;
          } else {
            temp[i][z] =
                min(temp[i][z], temp[i - 1][temp3[0][j]] + (x - z) * (x - z));
          }
        }
      }
      for (j = 0; j < count1; j++) {
        temp3[0][j] = temp3[1][j];
      }
      count0 = count1;
      count1 = 0;
    }

    mi = temp[N][temp3[0][0]];
    for (j = 1; j < count0; j++) {
      mi = min(temp[N][temp3[0][j]], mi);
    }

    cout << mi << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int foo(int huga) {
  if (huga <= 0)
    return 0;
  else if (huga >= 255)
    return 255;
  return huga;
}

int main() {
  int N, M, C[16], x, y = 128, i, j, k, l, mi;
  while (1) {
    int temp[20004][256] = {}, temp2[2][256] = {};
    temp2[0][128] = 1;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < M; i++) {
      cin >> C[i];
      temp2[1][foo(C[i] + 128)] = 1;
    }
    for (i = 1; i <= N; i++) {
      cin >> x;
      for (j = 0; j < 256; j++) {
        if (!temp2[1][j])
          continue;
        mi = 2147483647;
        for (k = 0; k < M; k++) {
          if (foo(j - C[k]) == 0) {
            for (l = 255 - C[k]; l <= 255; l++) {
              if (temp2[0][l]) {
                mi = min(temp[i - 1][l], mi);
              }
            }
          } else if (foo(j - C[k]) == 255) {
            for (l = 0; l <= -C[k]; l++) {
              if (temp2[0][l]) {
                mi = min(temp[i - 1][l], mi);
              }
            }
          } else {
            if (!temp2[0][foo(j - C[k])]) {
              continue;
            }
            mi = min(temp[i - 1][foo(j - C[k])], mi);
          }
        }
        temp[i][j] = mi + (x - j) * (x - j);
      }
      for (j = 0; j < 256; j++) {
        temp2[0][j] = temp2[1][j];
        temp2[1][j] = 0;
      }
      for (j = 0; j < 256; j++) {
        if (!temp2[0][j])
          continue;
        for (k = 0; k < M; k++) {
          temp2[1][foo(C[k] + j)] = 1;
        }
      }
    }
    mi = 2147483647;
    for (i = 0; i < 256; i++) {
      if (!temp2[0][i])
        continue;
      mi = min(mi, temp[N][i]);
    }
    cout << mi << endl;
  }
  return 0;
}