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

// #include "bits/stdc++.h"

#include <iostream>
#include <vector>
// #include <map>
// #include <string>
#include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

int n;
int m;
uint x;
vector<int> c;
bool bef = 0;
bool aft = 1;
const int size = 255 + 1;
vector<vector<uint>> vec(2, vector<uint>(size, -1));

inline uint mse(uint a, uint b) {
  uint diff = (a > b) ? (a - b) : (b - a);
  return diff * diff;
}

uint calcBefMin(int i) {
  uint min_val = -1;
  for (auto v : c) {
    int key = i - v;
    if (key < 0 or key > 255) {
      continue;
    }
    min_val = min(min_val, vec[bef][key]);
  }
  return min_val;
}

inline uint myplus(uint a, uint b) {
  uint m = -1;
  if (b <= m - a) { // check overflow
    return a + b;
  }
  return -1;
}

void solve() {
  c.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  int c_max = *std::max_element(begin(c), end(c));
  int c_min = *std::min_element(begin(c), end(c));
  for (int i = 0; i <= 255; i++) {
    vec[bef][i] = -1;
  }
  vec[bef][128] = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    int bef_key;
    // do
    for (bef_key = 1; bef_key <= 254; bef_key++) {
      vec[aft][bef_key] = myplus(calcBefMin(bef_key), mse(bef_key, x));
    }
    // do for 255
    bef_key = 255;
    uint min_val = -1;
    for (int key = bef_key - c_max; key <= 255; key++) {
      min_val = min(min_val, vec[bef][key]);
    }
    vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
    // do for 0
    bef_key = 0;
    min_val = -1;
    for (int key = bef_key - c_min; key >= 0; key--) {
      min_val = min(min_val, vec[bef][key]);
    }
    vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
    // for next
    bef = !bef;
    aft = !aft;
  }
  cout << *std::min_element(begin(vec[bef]), end(vec[bef]))
       << endl; // bef is correct, because swaped
}

int main() {

  while (true) {
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    solve();
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}

Error: MLE

Fixed submission:

// #include "bits/stdc++.h"

#include <cassert>
#include <iostream>
#include <vector>
// #include <map>
#include <queue>
#include <set>
#include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

struct Node;
struct Seeker;

const int first_signal_log = 128;
int n;
int m;
vector<short> book; // code book // TODO short
vector<short> seq;  // sequence // TODO short
priority_queue<Seeker> que;
set<Node> close_list;

template <class T> void print(const vector<T> &vec) {
  for (auto v : vec) {
    cout << " " << v;
  }
  cout << endl;
}

struct Node {
  string k = "";
  void push_back(int key) {
    assert(0 <= key);
    assert(key <= 16);
    k.push_back((char)key + 65);
  }

  void print() const {
    cout << "(";
    for (auto c : k) {
      cout << (int)c - 65 << " ";
    }
    cout << k;
    cout << ") ";
  }
};

bool operator<(const Node &a, const Node &b) { return a.k < b.k; }

bool operator==(const Node &a, const Node &b) { return a.k == b.k; }

struct Seeker {
  Node node;
  uint cost = 0;            // square sum
  int y = first_signal_log; // TODO short

  Seeker(Node _node) : node(_node) {}

  void print() const {
    node.print();
    cout << y << " " << cost << endl;
  }

  void change(int code) {
    y += code;
    y = max(0, y);
    y = min(y, 255);
  }
};

bool operator<(const Seeker &a, const Seeker &b) { return a.cost > b.cost; }

void seek(Seeker s, size_t key) {
  s.node.push_back(key);
  if (close_list.count(s.node) > 0) {
    return;
  }
  close_list.insert(s.node);
  size_t time = s.node.k.size();
  auto code = book[key];
  s.change(code); // s.y += code;
  auto diff = s.y - seq[time];
  s.cost += diff * diff;
  que.push(s);
  // cout << "seek s "; s.print(); // debug
}

bool isGoal(const Seeker &s) {
  int time = s.node.k.size();
  return n == time;
}

uint solve() {
  Node start_node;
  Seeker start(start_node);
  que.push(start);
  while (que.empty() == false) {
    Seeker s = que.top();
    // cout << "top s print "; s.print(); // debug
    if (isGoal(s)) {
      return s.cost;
    }
    que.pop();
    for (size_t key = 0; key < book.size(); key++) {
      seek(s, key);
    }
  }
  return -1;
}

void clearQue() {
  while (que.empty() == false) {
    que.pop();
  }
}

void input() {
  clearQue();
  close_list.clear();
  book.resize(m);
  for (auto &val : book) {
    cin >> val;
  }
  seq.resize(n + 1);
  seq[0] = first_signal_log;
  for (int i = 1; i < n + 1; i++) {
    cin >> seq[i];
  }
  // std::cout << "debug book"; print(book);
  // std::cout << "debug seq"; print(seq);
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    input();
    cout << solve() << endl;
  }

  // std::cout << "\\e[38;5;0m\\e[48;5;40m --- end ---  \\e[m" << std::endl; //
  // debug
  return 0;
}