Back
Close

Molar mass calculator

Statement

 Goal

A mole is a unit used in chemistry that which is 6.022e23 times (the Avogadro constant) of an element, or 6022 billion of billions times. For example, a mole of water contains 6.022e23 times the water molecule. A mole of rize would be 6.022e23 times a grain of rice, or 6022 billion of billions times, which is enough to feed the whole world for years. A mole is nothing more than a synonym for a quantity, as pair is a synonym for a quantity of 2.

A friend of your asked you to write a program that will calculates the molar mass of a molecule. She explains to you, the one who never listened in chemistry courses, that the molar mass is the mass of one mole of that molecule. And the molar mass of the molecule is the sum of all the molar mass of each atom multiplied by the number of that kind of atom.

You're not interested by chemistry, but it's the perfect occasion to sharpen your programming skills. She needs that program soon, like in less than 15 min. Will you succeed ?
Input
Line 1: An integer N for the amount of atoms with their molar mass.
N next lines: Two values atom, which is the symbol of the atom in the periodic table, and molarMass, which is the molar mass of that given atom, with 3 digits precision.
Line 2: The molecule to parse in order to calculate its molar mass.
Output
Line 1 : The molar mass of the molecule, with 3 digits precision, including trailing 0 if necessary.
Constraints
As in the periodic table, an atom can be composed of one or two letters.
The molecule to parse is guaranteed to be valid. No need to check for unknown atoms.
Example
Input
2
C 12.011
O 15.999
CO2
Output
44.009

Game modes
Fastest, Reverse

Test cases
Carbon Dioxyde Test
Input
2 C 12.011 O 15.999 CO2
Output
44.009

Water Validator
Input
2 H 1.008 O 15.999 H2O
Output
18.015

Abietic acid Test
Input
3 C 12.011 H 1.008 O 15.999 C19H29COOH
Output
302.458

Benzaldehyde Validator
Input
3 C 12.011 H 1.008 O 15.999 C6H5CHO
Output
106.124

Lysine Test
Input
4 C 12.011 H 1.008 N 14.007 O 15.999 C6H14N2O2
Output
146.190

Ethidum bromium Validator
Input
4 C 12.011 H 1.008 Br 79.904 N 14.007 C21H20BrN3
Output
394.316

Long one Test
Input
11 C 12.011 H 1.008 N 14.007 O 15.999 Cl 35.453 Br 79.904 S 32.066 Co 58.933 Fe 55.845 Pd 106.420 Hs 277.000 C13H3C7Br23HsONHs5Co6H4C4OPd82C2H5Fe31S9Cl12
Output
15395.442

Long one - Validator Validator
Input
11 C 12.011 H 1.008 N 14.007 O 15.999 Cl 35.453 Br 79.904 S 32.066 Co 58.933 Fe 55.845 Pd 106.420 Hs 277.000 C13H8C7Br30HsONHsFe45Co6C4OPd82C2H5Fe31S9Cl12
Output
17360.803

Solution language

Solution

Stub generator input