MMASS - Mass of Molecule


A molecule can be defined as a sequence of atoms and represented by a chemical formula consisting of letters denoting these atoms. E.g. letter H denotes atom of hydrogen, C denotes atom of carbon, O denotes atom of oxygen, formula COOH represents molecule consisting of one atom of carbon, two atoms of oxygen and one atom of hydrogen.

To write some formulas efficiently, we use the following rules. Letters denoting some atoms can be grouped by enclosing in parentheses, e.g. formula CH(OH) contains group OH. Groups can be nested – a group can also contain other groups. To simplify a formula, consecutive occurrences of the same letter can be replaced with that letter followed by a number of these occurrences. E.g. formula COOHHH can be written as CO2H3 and it represents a molecule consisting of one atom of carbon, two atoms of oxygen and three atoms of hydrogen. Furthermore, consecutive occurrences of the same group can be replaced with that group followed by a number of these occurrences. E.g. formula CH (CO2H) (CO2H) (CO2H) can be written as CH(CO2H)3 and molecule represented by both those formulas consists of four atoms of carbon, four atoms of hydrogen and six atoms of oxygen. A number written after a letter or a group is always greater than or equal to 2 and less than or equal to 9. A mass of a molecule is a sum of masses of all its atoms. One atom of hydrogen has mass 1, one atom of carbon has mass 12 and one atom of oxygen has mass 16.

Write a program that will calculate a mass of a molecule.

Input

The first and only line of input file contains a formula of a molecule whose mass needs to be determined. A formula of a molecule will consist of characters H, C, O, (, ) , 2, 3, ..., 9 only. Its length will be less or equal to 100 characters.

Output

The first and only line of output file should contain a mass of a molecule represented with a given formula. The result will always be less than or equal to 10,000.

Sample

MASS.IN

COOH

MASS.OUT

45
 
MASS.IN

CH(CO2H)3

MASS.OUT

148
 
MASS.IN

((CH)2(OH2H)(C(H))O)3

MASS.OUT

222
 


hide comments
Apy: 2017-11-22 14:59:47

Input has whitespaces. For Python users please use strip(). Cost me a lot of NZECs.

mdtgaur54: 2017-10-29 18:41:17

AC in 1 go, nice problem of stack. Must do.

aman22222: 2017-10-24 19:11:42

STACK IT IS!

Last edit: 2017-10-25 04:03:01
Addy: 2017-10-12 20:50:17

There is ONLY one line of input i.e the formula of the molecule.
Just output the mass of the molecule in a new line, thats it.

abhiraj703: 2017-10-04 16:21:13

what is the significance of MASS.IN and MASS.OUT what is its use? How we will take input

Sigma Kappa: 2017-07-03 15:30:24

To get Accepted: don't read till EOF, just read one line, print the output for it, and be done. I got many WAs with while ( 1 == scanf("%s",s) ) etc...

giriprasad kemburu: 2016-08-04 08:46:27

Nice use of stack.Solved using one stack.
My 18th on SPOJ :):):)

:.Mohib.:: 2014-12-27 13:16:58

Tricky test cases plzzz.....

avinash kumar: 2014-11-16 01:50:55

string contains whitespaces at the end for some test cases, using gets() i always got wa, just using scanf, i got it ac

Concerned Indian: 2014-06-25 21:07:42

what is the problem with test case #10...


Added by:~!(*(@*!@^&
Date:2009-03-08
Time limit:0.252s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:COI 01