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 <cstring>

using namespace std;

bool error = false;
int addr = 1, idx, M;
string line;
int var[26];
int size[100000];

int parse(){
	int res = 0;
	if(line[idx] == '('){
		idx++;
		res = parse();
		idx++;
	}
	else if(line[idx] == 'm'){
		idx += 7;
		int t = 0;
		while(isdigit(line[idx]))
			t = 10*t + line[idx++]-'0';
		if(t <= M){
			size[addr] = t;
			M -= t;
			res = addr++;
		} else {
			res = 0;
		}
		idx++;
	}
	else if(line[idx] == 'c'){
		idx += 6;
		res = parse();
		if(res == -1 || (res > 0 && size[res] == 0)) error = true;
		else if(res > 0 && size[res] <= M){
			size[addr] = size[res];
			M -= size[res];
			res = addr++;
		} else {
			res = 0;
		}
		idx++;
	}
	else if(line[idx] == 'f'){
		idx += 5;
		res = parse();
		if(res == -1 || (res > 0 && size[res] == 0)) error = true;
		else {
			M += size[res];
			size[res] = 0;
		}
	}
	else if(idx+1<line.size() && line[idx+1] == '='){
		int v = line[idx]-'A';
		idx += 2;
		res = var[v] = parse();
	}
	else if(idx+1<line.size() && line[idx+1] == 'U'){
		idx += 4;
		res = 0;
	}
	else {
		res = var[line[idx++]-'A'];
	}
	return res;
}

int main(){
	int heap; cin >> heap;
	M = heap;
	memset(var, -1, sizeof(var));
	memset(size, 0, sizeof(size));
	while(cin >> line){
		idx = 0;
		parse();
	}
	for(int i=0;i<26;i++)
		if(var[i]!=-1){
			M += size[var[i]];
			size[var[i]] = 0;
		}
	if(error) cout << "Error" << endl;
	else      cout << heap-M << endl;
}