import java.util.*;
public class Solution {
public long legalExp (String str) {
Deque<Long> stack = new ArrayDeque<>();
StringBuilder s = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if ('0'<=str.charAt(i)&&str.charAt(i)<='9') {
s.append(str.charAt(i));
continue;
} else if (str.charAt(i) == '#') {
long n = Long.parseLong(s.toString());
s.delete(0,s.length());
stack.push(n);
} else {
long a = stack.pop(),b = stack.pop();
if (str.charAt(i) == '+') {
stack.push(a+b);
} else if (str.charAt(i) == '-') {
stack.push(b-a);
}else{
stack.push(a*b);
}
}
}
return stack.pop();
}
}