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

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;
int n;
int m;
int a;

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cin >> m;
    if (m == 0) {
      break;
    }

    vector<int> codebook;
    vector<int> data;
    for (int i = 0; i < m; i++) {
      cin >> a;
      codebook.push_back(a);
    }
    for (int i = 0; i < n; i++) {
      cin >> a;
      data.push_back(a);
    }
    // 一個前のハッシュ値による最小誤差でハッシュ。

    // map<int,int> hash;
    int hash[256];
    for (int i = 0; i < 256; i++) {
      hash[i] = INT_MAX;
    }
    hash[128] = 0;

    int _mi;
    int _mindex = -1;
    for (int i = 0; i < n; i++) {
      int d = data[i]; // 次のデータ
      int next_hash[256];
      for (int j = 0; j < 256; j++) {
        next_hash[j] = INT_MAX;
      }
      // 最小値とdの間のみ保存。
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          if (hash[j] != INT_MAX) {
            int f = max(min(j + codebook[k], 255), 0);
            int div = f - d;
            int s = hash[j] + div * div;
            next_hash[f] = min(next_hash[f], s);
          }
        }
      }
      for (int j = 0; j < 256; j++) {
        hash[j] = next_hash[j];
      }
    }
    int mi = INT_MAX;
    for (int i = 0; i < 256; i++) {
      mi = min(hash[i], mi);
    }
    cout << mi << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;
int n;
int m;
int price;
int a;
int b;

struct G {
  string name;
  double value;
};
bool operator>(const G &left, const G &right) {
  if (left.value > right.value) {
    return true;
  } else if (left.value < right.value) {
    return false;
  } else {
    return left.name < right.name;
  }
}

bool operator<(const G &left, const G &right) {
  if (left.value > right.value) {
    return false;
  } else if (left.value < right.value) {
    return true;
  } else {
    return left.name > right.name;
  }
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cin >> m;
    if (m == 0) {
      break;
    }
    vector<int> codebook;
    vector<int> data;
    for (int i = 0; i < m; i++) {
      cin >> a;
      codebook.push_back(a);
    }
    for (int i = 0; i < n; i++) {
      cin >> a;
      data.push_back(a);
    }
    // 値,誤差値でハッシュ。
    map<int, int> hash;
    hash[128] = 0;

    for (int i = 0; i < n; i++) {
      int d = data[i]; // 次のデータ
      map<int, int>::iterator it = hash.begin();
      map<int, int> next_hash;
      map<int, int>::iterator next_hash_end = next_hash.end();
      while (it != hash.end()) {
        for (int k = 0; k < m; k++) {
          int f = max(min((*it).first + codebook[k], 255), 0);
          int div = f - d;
          int s = (*it).second + div * div;
          if (next_hash.find(f) != next_hash_end) {
            next_hash[f] = min(next_hash[f], s);
          } else {
            next_hash[f] = s;
          }
        }
        ++it;
      }

      hash = next_hash;
    }
    int mi = INT_MAX;
    map<int, int>::iterator it = hash.begin();
    while (it != hash.end()) {
      mi = min((*it).second, mi);
      it++;
    }
    cout << mi << endl;
  }
}