Language: C++

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;

const int nil = -1;

bool vis[100000];
int M, ref[26], p, m[100000], ps;

typedef pair<char, int> Result;

void line();
Result expr();
Result number();

string str;
void line()
{
	if(str[p] == 'f') {
		p += 5;
		Result r = expr();
		if(r.first != nil) {
			if(ref[r.first - 'A'] == nil) { throw 0; }
			else ref[r.first - 'A'] = nil;
		}
		else {
			//nil
		}
		p += 1;
	}
	else {
		expr();
	}
}

Result expr()
{
	Result l;
	if(str[p] == '(') {
		p++;
		l = expr();
		p++;
	}
	else if(isupper(str[p]) && str[p+1] != 'U') {
		l = make_pair(str[p], nil);
		
		p++; 
		
		if(str[p] == '=') {
			p++;
			Result r = expr();
			if(r.first != nil) {
				ref[l.first - 'A'] = ref[r.first - 'A'];
			}
			else {
				ref[l.first - 'A'] = ps;
				m[ps] = r.second;
			}
		}
	}
	//NULL
	else if(isupper(str[p])) {
		p += 4;
		l = make_pair(nil, nil);
	}
	
	//malloc
	else if(str[p] == 'm') {
		p += 7;
		l = number();
		p++;
		if(M < l.second) l = make_pair(nil, nil);
		else M -= l.second;
	}
	
	//clone
	else if(str[p] == 'c') {
		p += 6;
		l = expr();
		p++;
		if(l.first != nil) {
			if(ref[l.first - 'A'] == nil) { throw 0; }
			else l = make_pair(nil, m[ref[l.first - 'A']]);
		}
		else {
			if(l.second == nil) l = make_pair(nil, nil);
			else {
				if(M < l.second) l = make_pair(nil, nil);
				else M -= l.second;
			}
		}
	}

	return l;
}

Result number()
{
	int res = 0;
	while(isdigit(str[p])) {
		res *= 10;
		res += str[p] - '0';
		p++;
	}

	return make_pair(-1, res);
}

int main()
{
	cin >> M;
	int S = M;

	for(int i=0; i<26; i++) ref[i] = nil;
	memset(m, 0, sizeof(m));
	memset(vis, 0, sizeof(vis));
	
	string s;
	vector<string> cmd;
	while(cin >> s) cmd.push_back(s);

	try {
		for(int i=0; i<cmd.size(); i++) {
			str = cmd[i];
			p = 0;

			line();
		}

		for(int i=0; i<26; i++) {
			if(ref[i] == nil) continue;
			if(vis[ref[i]]) continue;

			M += m[ref[i]];
			vis[ref[i]] = true;
		}

		cout << S - M << endl;
	}
	catch(...) {
		cout << "Error" << endl;
	}
}

Compiler output:
a.cc: In function 'void line()':
a.cc:25:28: error: reference to 'ref' is ambiguous
   25 |                         if(ref[r.first - 'A'] == nil) { throw 0; }
      |                            ^~~
In file included from /usr/include/c++/14/string:52,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:26:30: error: reference to 'ref' is ambiguous
   26 |                         else ref[r.first - 'A'] = nil;
      |                              ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc: In function 'Result expr()':
a.cc:55:33: error: reference to 'ref' is ambiguous
   55 |                                 ref[l.first - 'A'] = ref[r.first - 'A'];
      |                                 ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:55:54: error: reference to 'ref' is ambiguous
   55 |                                 ref[l.first - 'A'] = ref[r.first - 'A'];
      |                                                      ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:58:33: error: reference to 'ref' is ambiguous
   58 |                                 ref[l.first - 'A'] = ps;
      |                                 ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:84:28: error: reference to 'ref' is ambiguous
   84 |                         if(ref[l.first - 'A'] == nil) { throw 0; }
      |                            ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:85:51: error: reference to 'ref' is ambiguous
   85 |                         else l = make_pair(nil, m[ref[l.first - 'A']]);
      |                                                   ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc: In function 'int main()':
a.cc:116:33: error: reference to 'ref' is ambiguous
  116 |         for(int i=0; i<26; i++) ref[i] = nil;
      |                                 ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:117:9: error: 'memset' was not declared in this scope
  117 |         memset(m, 0, sizeof(m));
      |         ^~~~~~
a.cc:5:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    4 | #include <vector>
  +++ |+#include <cstring>
    5 | using namespace std;
a.cc:133:28: error: reference to 'ref' is ambiguous
  133 |                         if(ref[i] == nil) continue;
      |                            ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:134:32: error: reference to 'ref' is ambiguous
  134 |                         if(vis[ref[i]]) continue;
      |                                ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:136:32: error: reference to 'ref' is ambiguous
  136 |                         M += m[ref[i]];
      |                                ^~~
/usr/include/c++/14/bits/refwrap.h:445:5: note: candidates are: 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(reference_wrapper<_Tp>)'
  445 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/refwrap.h:436:10: note:                 'template<class _Tp> void std::ref(const _Tp&&)'
  436 |     void ref(const _Tp&&) = delete;
      |          ^~~
/usr/include/c++/14/bits/refwrap.h:425:5: note:                 'template<class _Tp> std::reference_wrapper<_Tp> std::ref(_Tp&)'
  425 |     ref(_Tp& __t) noexcept
      |     ^~~
a.cc:10:8: note:                 'int ref [26]'
   10 | int M, ref[26], p, m[100000], ps;
      |        ^~~
a.cc:137:29: error: reference to 'ref' is ambiguous
  137 |                         vis[ref[i]] = true;
      |