Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

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


Output

0
#include <iostream>
#include <string>
#include <map>
#include <cassert>
using namespace std;

map<int,int> heap; // 
map<char,int> var; // value of variable
int hsize,maxhs,ridx,rvalue;
string in;

bool expr(int idx) {
    if(idx+1 < in.length() && in[idx+1] == '=') { // assignment
        bool inner = expr(idx+2);
        var[in[idx]] = rvalue;
        return inner;
    }else if(in[idx] == 'm') {// malloc
        int s = 0,tidx = idx+7;
        while(isdigit(in[tidx])) {
            s *= 10;
            s += in[tidx] - '0';
            tidx++;
        }

        ridx = tidx+1;
        if(hsize + s > maxhs) rvalue = -1;
        else{
            hsize += s;
            rvalue = heap.size();
            heap[rvalue] = s;
        }

    }else if(in[idx] == 'c') {// clone
        bool inner = expr(idx+6);
        assert(in[ridx] == ')');

        ridx++;
        if(!inner) return false;
        if(rvalue == -1) return true; // NULL
        if(rvalue == -2 || heap.find(rvalue) == heap.end())
            return false;

        int s = heap[rvalue];
        if(hsize+s > maxhs) rvalue = -1;
        else{
            rvalue = heap.size();
            heap[rvalue] = s;
            hsize += s;
        }

    }else if(idx+1 < in.length() && in[idx] == 'N' && in[idx+1] == 'U') {// NULL
        ridx = idx+4;
        rvalue = -1;
    }else if(in[idx] == '(') {
        bool inner = expr(idx+1);
        ridx++;
        return inner;
    }else{ // variable
        ridx = idx+1;
        rvalue = var[in[idx]];
    }
    return true;
}

bool line() {
    if(in[0] == 'f'){// free
        bool inner = expr(5);

        if(!inner) return inner;
        if(rvalue == -1) return true;
        if(heap.find(rvalue) == heap.end()) return false;

        hsize -= heap[rvalue];
        heap.erase(heap.find(rvalue));
        return true;
    }else
        return expr(0);
}

int main() {
    cin>>maxhs;

    heap.clear(); var.clear();
    for(char c = 'A'; c <= 'Z'; ++c)
        var[c] = -2;

    bool haderror = false;
    while(cin>>in) if(!line()) haderror = true;

    if(haderror) {
        cout<<"Error"<<endl;
        return 0;
    }

    int ans = 0;
    for(map<int,int>::iterator it = heap.begin(); it != heap.end(); ++it) {
        if(it->second < 0) continue;
        bool refed = false;
        for(map<char,int>::iterator it2=var.begin(); it2 != var.end(); ++it2) {
            if(it2->second == it->first) {
                refed = true;
                break;
            }
        }
        if(!refed) ans += it->second;
    }

    cout<<ans<<endl;
}