Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

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


Output

0
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
int heap[5100];
using namespace std;
string in;
int v[26];
int cap;
int m;
int parser(int);
int alc = -1;
int free(int pos) {
	int adr = parser(pos+5);
	if (adr == -2)throw new exception;
	if (adr == -1)return 0;
	if (heap[adr] == -1)throw new exception;
	cap -= heap[adr];
	heap[adr] = -1;
	return 0;
}
int malloc(int pos) {
	int size = parser(pos + 7);
	if (cap +size> m) {
		return -1;
	}
	alc++;
	heap[alc] = size;
	cap += size;
	return alc;
}
int clone(int pos) {
	int adr = parser(pos + 6);
	if (adr == -2)throw new exception;
	if (adr == -1)return -1;
	if (cap +heap[adr]> m) {
		return -1;
	}
	if (heap[adr] == -1)throw new exception;
	alc++;
	heap[alc] = heap[adr];
	cap += heap[adr];
	return alc;
}
int assign(int pos) {
	return v[in[pos] - 'A'] = parser(pos + 2);
}
int literal(int pos) {
	if (in[pos + 1] == '=')return assign(pos);
	if (in[pos + 1] == 'U')return -1;
	if (in[pos] >= 'A') {
		return v[in[pos] - 'A'];
	}
	int num=0;
	for (; in[pos] != ')'; pos++) {
		num *= 10;
		num += in[pos] - '0';
	}
	return num;
}
int parser(int pos) {
	for (;;) {
		switch (in[pos]) {
		case 'c':
			return clone(pos);
		case 'f':
			return free(pos);
		case 'm':
			return malloc(pos);
		case '(':
			pos++;
			continue;
		default:
			return literal(pos);
		}
	}
}
int main(){
	cin >> m;
	fill(v, v + 26, -2);
	try {
		while (cin >> in) {
			parser(0);
		}
	}
	catch(...){
		cout << "Error" << endl;
		return 0;
	}
	for (int a : v) {
		if (a >= 0) {
			heap[a] = 0;
		}
	}
	int sum = 0;
	for (int a : heap)sum += a > 0 ? a : 0;
	cout << sum << endl;
	return 0;
}