Parse a short chemical formula, look up each element's atomic mass, multiply by any following count, and print the total with two decimals.
Easy2StringHash mapImplementationSimulationInterviewNo attempts yetTime limit1sMemory limit512 MBA chemical element is written as a symbol. A symbol is either one uppercase letter or an uppercase letter followed by a lowercase letter. For example, H is hydrogen and He is helium. A compound is several elements written one after another. An integer right after an element symbol says how many times that element is used. H2O is two hydrogen and one oxygen, and CH4 is one carbon and four hydrogen. The same symbol can appear at several places in one compound. Acetic acid is written CH3COOH.
The atomic mass of an element is the mass of one atom of that element. The molecular mass of a compound is the sum of the atomic masses of the elements used in it. If the atomic mass of hydrogen (H) is 1.01 and the atomic mass of oxygen (O) is 16.00, then the molecular mass of water (H2O) is 2×1.01+16.00=18.02.
Given a compound, compute its molecular mass.
The first line contains the number of elements n (1≤n≤20).
Each of the next n lines contains an element symbol and the atomic mass m (0.01≤m≤500.00) of that element, separated by a space. A symbol is either one uppercase letter or an uppercase letter followed by a lowercase letter. No symbol is given twice. Every atomic mass is written with exactly two digits after the decimal point.
The last line contains the compound whose molecular mass you have to compute. The compound is a string of element symbols and integers. Each integer comes directly after the symbol it applies to and is between 2 and 100 inclusive. The length of the compound is between 1 and 25 characters inclusive. Every element that appears in the compound is one of the n elements listed above. Compounds with parentheses, such as Al2(SO4)3, are not given.
Print the molecular mass of the compound with exactly two digits after the decimal point. Every atomic mass has two decimal digits and every count is an integer, so the molecular mass is exact at two decimal places. Do not drop a trailing zero. For example, if the molecular mass is 7, print 7.00.