## DMOPC '15 Contest 5 P3 - All Your Base

View as PDF

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Okabe likes writing notes for himself. Since he writes about strange things and does not want others to think he is weird, he encodes all his messages in his own super-secret format. Unfortunately for Okabe, you — being an amazing cryptanalyst — have figured out his scheme.

First, he maps a character to an integer , and then converts it into another integer by changing its base. He creates a random list of integers, where is stored in base , and is stored in base . That is,

You've gotten your hands on some of Okabe's notes, and would like to decrypt some values, given . Can you do it?

#### Reminder

The notation for representing a number in base is .

#### Input Specification

The first line of input will contain the integers and .
The next and final line of input will contain space-separated integers making up , with the -th integer representing . is always given in base , and it is guaranteed that can be converted (for every digit in , unless , when ).

#### Output Specification

A single integer, .

#### Sample Input 1

5 3
10 10 10

#### Sample Output 1

5

#### Sample Input 2

5000 4
7 1001 10 2

#### Sample Output 2

1715

#### Explanation

Working your way up from ,

Step-by-step,
.
.
.
.