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.


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.


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.












hide comments
king_bradley_2: 2019-05-21 08:58:12

AC in one go :))

pistachio: 2019-04-21 21:49:23

There are space separated formulas in test cases. You have to calculated the mass of the first one.

sandeep48: 2019-02-21 19:46:20

AC in one go:
try to work in only int stack and
simple implementation of stack

y17prashant: 2019-02-18 01:33:27

Dont hard code. Simple Stack Implementation :)

ilovemetoo: 2018-11-24 09:31:00

@code_man : You are failing test case 10 because if a digit occurs and there isn't a bracket before it, digit*element before it mass should be added. Make sure you did that.
For people looking for answer and a well-written editorial, head over to:

jinayshah86: 2018-11-20 08:25:53

@code_man 54

be1035016: 2018-06-22 21:13:16

good problem

code_man: 2018-05-22 11:39:16

whats test case 10 im failing 5th time can anyone specify corner case

code_man: 2018-05-22 10:12:30

(((HH)3)3)3 what is right output for this formulae according to the question??
18 or 54??

hg3994: 2018-05-01 09:58:51

All my test cases are passing but still WA. Can anyone please help me?
Thanks a lot.

Added by:~!(*(@*!@^&
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