Costly Phone Number
Practice
4.1 (47 votes)
Easy
Graphs
Math
Shortest path algorithm
Problem
86% Success 3883 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A cell phone company is trying out its new model of cell phone. Here's how its structure is:

The keypad has 11 buttons corresponding to digits from 0 to 9 and one additional button called Add. After pressing any button from 0 to 9, the corresponding digit appears on the screen. The Add button replaces the last two digits appearing on the screen with their sum taken modulo 10. (See sample test for more clarity). If there are less than two digits currently on the screen, pressing Add does nothing.

Each button has a non-negative cost of pressing associated with it. The cost of pressing Add button is always 0. Given the cost of pressing each button and the target phone number, find the minimum cost of feeding that number into the phone screen using a sequence of button presses.

INPUT

The first line of input file consists of an integer T, which indicates the number of test cases. Then the description of T test cases follow. Each test case is described by 3 lines. The first of them contains 10 space separated integers, denoting the cost of pressing buttons from 0 to 9. The second line contains the length of the target phone number. The third line contains the target phone number S itself.

OUTPUT

Print the minimum cost of feeding the phone screen with the target number for each test case in a separate line.

CONSTRAINTS

1 ≤ T ≤ 1000
0 ≤ Cost of any button ≤ 1000
1 ≤ |S|≤ 1000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
Depth First SearchEasyFloyd Warshall AlgorithmGraphs
Points:30
33 votes
Tags:
ApprovedEasyFloyd Warshall AlgorithmGraphsOpenShortest Path Algorithm
Points:30
14 votes
Tags:
AlgorithmsDijkstra's AlgorithmEasyGraphsShortest Path Algorithm