def tokenize(s):
	tok_list = []

	state = 'start'
	i = 0
	while i < len(s):
		c = s[i]

		if state == 'start':
			if c == '(' or c == ')' or c == '+' or c == '-':
				tok_list.append(c)
			elif c == ' ':
				pass
			elif c.isdigit():
				state = 'number'
				number = c
			else:
				pass  # invalid expression
		elif state == 'number':
			if c.isdigit():
				number += c
			else:
				tok_list.append(int(number))
				state = 'start'
				continue

		i += 1

	if state == 'number': tok_list.append(int(number))

	return tok_list

def do_calculate(tok_list):
	stack = list()

	def do_number(tok):
		if len(stack) == 0 or stack[-1] == '(':
			stack.append(tok)
		elif stack[-1] == '+':
			stack.pop()
			stack[-1] += tok
		elif stack[-1] == '-':
			stack.pop()
			stack[-1] -= tok
		else:
			pass  # invalid expression			

	for tok in tok_list:
		if tok == '(' or tok == '+' or tok == '-':
			stack.append(tok)
		elif tok == ')':
			tok = stack.pop()  	# should be a number
			stack.pop() 		# should be '('	
			do_number(tok)	
		else:  # is a number
			do_number(tok)	


	return stack[0]

def calculate(s):
	tok_list = tokenize(s)
	return do_calculate(tok_list)

class Solution(object):
	def calculate(self, s):
		"""
		:type s: str
		:rtype: int
		"""
		return calculate(s)

sol = Solution()
print sol.calculate('1 + 1')
# print sol.calculate('(1+(4+5+2)-3)+(6+8)')
# print sol.calculate('0')
# print sol.calculate('(1+(4+5+2)-3)+(6+88)')
