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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

int M;
int rest;

map<char, int> val;
vector<int> memory;

char expr(const string &s);

void error() {
  cout << "Error" << endl;
  exit(0);
}

char assign(const string &s) {
  val[s[0]] = val[expr(s.substr(2))];
  return s[0];
}

char malloc(const string &s) {
  int mem = atoi(s.substr(7).c_str());
  if (mem > rest)
    return 'n';
  rest -= mem;
  memory.push_back(mem);
  val['-'] = memory.size() - 1;
  return '-';
}

char clone(const string &s) {
  char c = expr(s.substr(6, s.size() - 7));
  if (val[c] == 0)
    return c;
  if (val[c] == -1)
    error();
  else {
    int mem = memory[val[c]];
    if (mem > rest)
      return 'n';
    rest -= mem;
    memory.push_back(mem);
    val['-'] = memory.size() - 1;
  }
  return '-';
}

char expr(const string &s) {
  if (s.size() == 1)
    return s[0];
  if (s[1] == '=')
    return assign(s);
  if (s[0] == '(')
    return expr(s.substr(1, s.size() - 2));
  if (s[0] == 'm')
    return malloc(s);
  if (s[0] == 'c')
    return clone(s);
  return 'n';
}

void free(const string &s) {
  char c = expr(s.substr(5, s.size() - 6));
  if (val[c] == 0)
    return;
  if (val[c] == -1)
    error();
  else {
    rest += memory[val[c]];
    memory[val[c]] = 0;
    val[c] = -1;
  }
}

void line(const string &s) {
  if (s[0] == 'f') {
    free(s);
  } else {
    expr(s);
  }
}

void init() {
  for (int i = 'A'; i <= 'Z'; i++) {
    val[i] = -1;
  }
  val['-'] = -1;
  val['n'] = 0;
  memory.push_back(0);
  rest = M;
}

int check() {
  for (int i = 'A'; i <= 'Z'; i++) {
    if (val[i] != -1) {
      string s = "free";
      free(s + '(' + (char)i + ')');
    }
  }

  return M - rest;
}

int main() {
  cin >> M;
  init();
  string s;
  while (cin >> s)
    line(s);

  cout << check() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

bool error;
int M;
int rest;

map<char, int> val;
vector<int> memory;

char expr(const string &s);

char assign(const string &s) {
  val[s[0]] = val[expr(s.substr(2))];
  return s[0];
}

char malloc(const string &s) {
  int mem = atoi(s.substr(7).c_str());
  if (mem > rest)
    return 'n';
  rest -= mem;
  memory.push_back(mem);
  val['-'] = memory.size() - 1;
  return '-';
}

char clone(const string &s) {
  char c = expr(s.substr(6, s.size() - 7));
  if (val[c] == 0)
    return c;
  if (val[c] == -1)
    error = true;
  else {
    int mem = memory[val[c]];
    if (mem > rest)
      return 'n';
    rest -= mem;
    memory.push_back(mem);
    val['-'] = memory.size() - 1;
  }
  return '-';
}

char expr(const string &s) {
  if (s.size() == 1)
    return s[0];
  if (s[1] == '=')
    return assign(s);
  if (s[0] == '(')
    return expr(s);
  if (s[0] == 'm')
    return malloc(s);
  if (s[0] == 'c')
    return clone(s);
  return 'n';
}

void free(const string &s) {
  char c = expr(s.substr(5, s.size() - 6));
  if (val[c] == 0)
    return;
  if (val[c] == -1)
    error = true;
  else {
    rest += memory[val[c]];
    memory[val[c]] = 0;
    val[c] = -1;
  }
}

void line(const string &s) {
  if (s[0] == 'f') {
    free(s);
  } else {
    expr(s);
  }
}

void init() {
  for (int i = 'A'; i <= 'Z'; i++) {
    val[i] = -1;
  }
  val['-'] = -1;
  val['n'] = 0;
  memory.push_back(0);
  rest = M;
}

int check() {
  for (int i = 'A'; i <= 'Z'; i++) {
    if (val[i] != -1) {
      string s = "free";
      free(s + '(' + (char)i + ')');
    }
  }

  return M - rest;
}

int main() {
  cin >> M;
  init();
  string s;
  while (cin >> s)
    line(s);

  if (error)
    cout << "Error" << endl;
  else
    cout << check() << endl;

  return 0;
}