#include <stdio.h>
#include <stdlib.h>

#define N_MAX 100

enum type {
	TYPE_NONE,
	TYPE_VAL,
	TYPE_PLUS,
	TYPE_MINUS,
	TYPE_MULT,
	TYPE_NUM
};

enum state {
	STATE_ERROR,
	STATE_REPEAT,
	STATE_LAST,
};

struct token {
	int type;
	int value;
};

void
parse(struct token *out, char *arg)
{
	switch (arg[0])
	{
	case '\0':
		out->type = TYPE_NONE;
		out->value = 0;
		return;
	case '-':
		if (arg[1] == '\0') {
			out->type = TYPE_MINUS;
			out->value = 0;
		} else {
			out->type = TYPE_VAL;
			out->value = strtol(arg, NULL, 0);
		}
		break;
	case '+':
		if (arg[1] == '\0') {
			out->type = TYPE_PLUS;
			out->value = 0;
		} else {
			out->type = TYPE_VAL;
			out->value = strtol(arg, NULL, 0);
		}
		break;
	case '*':
		out->type = TYPE_MULT;
		out->value = 0;
		break;
	default:
		out->type = TYPE_VAL;
		out->value = strtol(arg, NULL, 0);
		break;
	}
}

enum state
get_token(struct token *out)
{
	char buf[16];
	char c;
	int p = 0;

	while ((c = getchar())) {
		switch (c) {
		case ' ':
			buf[p] = '\0';
			parse(out, buf);
			return STATE_REPEAT;
		case '\n':
		case '\r':
			buf[p] = '\0';
			parse(out, buf);
			return STATE_LAST;
		default:
			if (p >= 16) {
				return STATE_ERROR;
			}
			buf[p++] = c;
		}
	}

	return STATE_ERROR;
}

void
run_plus(struct token *p1, struct token *p2)
{
	p1->type = TYPE_VAL;
	p1->value = p1->value + p2->value;
}

void
run_minus(struct token *p1, struct token *p2)
{
	p1->type = TYPE_VAL;
	p1->value = p1->value - p2->value;
}

void
run_mult(struct token *p1, struct token *p2)
{
	p1->type = TYPE_VAL;
	p1->value = p1->value * p2->value;
}

int
main(void)
{
	struct token stack[N_MAX];
	int p = 0;
	int s;

#define RUN2OP(op) do {\
	if (p >= 2) {\
		run_##op(stack + p - 2, stack + p - 1);\
		p = p - 1;\
	} else {\
		goto end;\
	}\
} while(0)

	while ((s = get_token(&stack[p])) != STATE_ERROR) {
		switch (stack[p].type) {
		case TYPE_VAL:
			p++;
			break;
		case TYPE_PLUS:
			RUN2OP(plus);
			break;
		case TYPE_MINUS:
			RUN2OP(minus);
			break;
		case TYPE_MULT:
			RUN2OP(mult);
			break;
		}
		if (s == STATE_LAST)
			break;
	}

#undef RUN2OP

end:
	if (p)
		printf("%d\n", stack[p - 1].value);

	return 0;
}

