Robbie Can't Do Math


Submit solution

Points: 6 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

Robbie the Ram has a grade 1 math test tomorrow and is currently practicing review questions. However, he is very lazy and he doesn't want to actually do the questions. So he wants you to write a program for him that can calculate a math expression \(S\). However, since he is in grade 1, there is no multiplication or division, only addition, subtraction, and parentheses.

Constraints

\(1 \le |S| \le 3\times10^{5}\)
\(S\) contains spaces, +, -, (, ), and numbers.
There will be no two operations placed side by side. All parentheses will be placed in a way that is syntactically valid.

Input Specification

The first line of input will contain \(S\).

Output Specification

Output the result of evaluating the expression.

Sample Input 1

1+2

Sample Output 1

3

Sample Input 2

   ( 1 +  2  )-  ( 2 + 5)

Sample Output 2

-4

Sample Input 3

(1+(4+5+2)-3)+(6+8)

Sample Output 3

23

Sample Input 4

-(6 7 + 6 7)

Sample Output 4

-134

Explanation for Sample 4

The subtraction operator may be used as an unary operator. However, the addition operator will not be used as one.


Comments


  • 0
    Deo  commented on Nov. 7, 2025, 9:52 p.m.

    one liner in python

    • 🤑orz

  • 0
    Benjamin_Li  commented on Oct. 10, 2025, 9:59 p.m.

    67