Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

100
A=malloc(10)
B=clone(A)
free(A)


Output

0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;

int n, idx; //ãÀ, ¶ñÌY
int max_ptr, mem; //gpÏÝÌAhXl+1, »ÝmÛµÄ¢éÌTCY
char s[1000002]; //¶ñ

int var[256]; //eÏÉüÁÄ¢élði[
bool mem_flg[1000002]; //ãÉmÛµ½ÌæªcÁÄ¢é©Ç¤©
int mem_size[1000002]; //mÛµ½ÌæÌTCY

int Number();
int Expr();
int Assign();
int Malloc();
bool Free();
int Clone();
bool Line();

int Number(){
  int res = 0;

  while(isdigit(s[idx])){
    res *= 10;
    res += s[idx] - '0';
    idx++;
  }

  return res;
}

int Malloc(){
  idx += 7; //"malloc("Ì¶

  int tmp = Number();

  idx++; //")"Ì¶

  if(n < mem + tmp){
    return 0;
  }

  mem_flg[max_ptr] = true;
  mem_size[max_ptr] = tmp;
  mem += tmp;
  max_ptr++;

  return max_ptr - 1;
}

int Assign(){
  int alpha = s[idx];

  idx += 2; //Ï¼Æ"="Ì¶

  int ptr = Expr();

  return var[alpha] = ptr;
}

int Clone(){
  idx += 6; //"clone("Ì¶

  int ptr = Expr();
  if(ptr == 0){
    return 0;
  }
  if(ptr == -2 || ptr == -1 || !mem_flg[ptr]){
    return -2;
  }

  idx++; //")"Ì¶

  if(n < mem + mem_size[ptr]){
    return 0;
  }

  mem_flg[max_ptr] = true;
  mem_size[max_ptr] = mem_size[ptr];
  mem += mem_size[ptr];
  max_ptr++;

  return max_ptr - 1;
}

int Expr(){
  if(s[idx] == '('){
    idx++; //"("Ì¶
    int tmp = Expr();
    idx++; //")"Ì¶

    return tmp;
  }
  else if(s[idx] == 'N' && s[idx + 1] == 'U'){
    idx += 4; //"NULL"Ì¶
    return 0;
  }
  else if(s[idx + 1] == '='){
    return Assign();
  }
  else if(s[idx] == 'm'){
    return Malloc();
  }
  else if(s[idx] == 'c'){
    return Clone();
  }
  else{
    return var[(int)s[idx++]];
  }
}

bool Free(){
  idx += 5; //"free("Ì¶

  int ptr = Expr();
  if(ptr == -2 || ptr == -1){
    return false;
  }

  idx++; //")"Ì¶

  if(ptr == 0){ //NULLÌê, ³
    return true;
  }
  else{
    if(!mem_flg[ptr]){ //·ÅÉmÛÌæÅÈ¢ÈçError
      return false;
    }
    mem -= mem_size[ptr];
    mem_flg[ptr] = false;
    mem_size[ptr] = 0;
    return true;
  }
}

bool Line(){
  if(s[idx] == 'f'){
    return Free();
  }
  else{
    int tmp = Expr();
    if(tmp == -2){
      return false;
    }
    return true;
  }
}

int main(){
  bool error_flg = false;

  mem = 0;
  max_ptr = 1;
  memset(var, -1, sizeof(var));
  memset(mem_flg, 0, sizeof(mem_flg));
  memset(mem_size, 0, sizeof(mem_size));

  scanf("%d", &n);

  while(scanf("%s", s) != EOF){
    if(error_flg) continue;

    idx = 0;
    error_flg = !Line();
  }

  if(error_flg){
    printf("Error\n");
  }
  else{
    int ans = 0;

    for(int i = 'A'; i <= 'Z'; i++){
      if(0 < var[i]){
	mem_flg[var[i]] = false;
      }
    }

    for(int i = 1; i < max_ptr; i++){
      if(mem_flg[i]){
	ans += mem_size[i];
      }
    }

    printf("%d\n", ans);
  }
}