Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

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


Output

0
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
class MemoryError{};

struct Result{
  int value, p;
  Result(int v, int p) : value(v), p(p) {}
};

int variable[30];
int memory[100001];
int memory_length;
const int MEMORY_MAX = 100000;
int HEAPSIZE; //残り使用可能なメモリ量

int malloc_memory(int size){
  if(HEAPSIZE >= size){
    assert(memory_length >= 1 && memory_length < MEMORY_MAX);
    HEAPSIZE -= size;
    memory[memory_length++] = size;
    return memory_length - 1;
  }else{
    return 0; // NULL
  }
}

Result line(const string& s, int p);
Result expr(const string& s, int p);
Result assign(const string& s, int p);
Result malloc(const string& s, int p);
Result free(const string& s, int p);
Result clone(const string& s, int p);

Result line(const string& s, int p){
  if(s.substr(p, 4) == "free"){
    return free(s, p);
  }else{
    return expr(s, p);
  }
}

Result expr(const string& s, int p){
  if(s[p] == '('){
    Result r = expr(s, p + 1);
    assert(s[r.p] == ')');
    r.p ++;
    return r;
  }else if(s.substr(p, 4) == "NULL"){
    return Result(0, p + 4);
  }else if(s.substr(p, 6) == "malloc"){
    return malloc(s, p);
  }else if(s.substr(p, 5) == "clone"){
    return clone(s, p);
  }else if(isupper(s[p])){
    if(s[p + 1] == '='){
      Result r = expr(s, p + 2);
      variable[s[p] - 'A'] = r.value;
      return r;
    }else{
      return Result(variable[s[p] - 'A'], p + 1);
    }
  }else{
    assert(false);
  }
}

Result malloc(const string& s, int p){
  assert(s.substr(p, 7) == "malloc(");
  p += 7;
  int size = 0;
  while(isdigit(s[p])){
    size = size * 10 + s[p++] - '0';
  }
  assert(s[p] == ')');
  p++;
  int pointer = malloc_memory(size);
  return Result(pointer, p);
}

Result free(const string& s, int p){
  assert(s.substr(p, 5) == "free(");
  p += 5;
  Result r = expr(s, p);
  assert(s[r.p] == ')');
  r.p++;
  if(memory[r.value] == -1){
    throw MemoryError();
  }else if(memory[r.value] == 0){
    // NULL
    return Result(0, r.p);
  }else{
    HEAPSIZE += memory[r.value];
    memory[r.value] = -1;
    return r;
  }
}

Result clone(const string& s, int p){
  assert(s.substr(p, 6) == "clone(");
  p += 6;
  Result r = expr(s, p);
  assert(s[r.p] == ')');
  r.p++;
  if(memory[r.value] == -1){
    throw MemoryError();
  }else if(memory[r.value] == 0){
    // NULL
    return Result(0, r.p);
  }else{
    int pointer = malloc_memory(memory[r.value]);
    return Result(pointer, r.p);
  }
}

int main(){
  memset(memory, -1, sizeof(memory));
  memory_length = 1;
  memory[0] = 0;
  REP(i, 30) variable[i] = MEMORY_MAX;

  cin >> HEAPSIZE;
  string s;
  try{
    while(cin >> s){
      line(s, 0);
    }
    REP(i, 30) memory[variable[i]] = 0;
    int ans = 0;
    REP(i, memory_length) if(memory[i] > 0){
      ans += memory[i];
    }
    cout << ans << endl;
  }catch(...){
    cout << "Error" << endl;
  }

  return 0;
}