#P11188. 「KDOI-10」商店砍价

    ID: 10265 远端评测题 1000ms 512MiB 尝试: 1 已通过: 1 难度: 6 上传者: 标签>动态规划 DP2024洛谷原创O2优化洛谷月赛

「KDOI-10」商店砍价

Description

There is an integer nn consisting only of digits 191\sim 9.

You can do an arbitrary number of operations on nn (possibly zero):

  • Choose a digit of nn and delete it. Suppose the digit is xx, this operation will cost vxv_x. Note that after this operation, the length of nn decreases by 11, and the value of nn also changes;
  • Delete all the remaining digits of nn. This operation will cost nn.

Find the minimum cost to delete all digits of nn.

Input Format

Each test contains multiple test cases.

The first line of the input contains a single integer cc — the id of the test. c=0c=0 represents that this is a sample test.

The second line contains a single integer tt — the number of test cases.

For each test case:

  • The first line contains a single integer nn — the initial value of the integer.
  • The second line contains nine integers v1,v2,,v9v_1,v_2,\ldots,v_9 — the cost of deleting each digit.

Output Format

For each test case:

  • Print a single integer in the only line of the output — the minimum cost.
0
3
123
10 10 10 10 10 10 10 10 10 
1121
2 1 2 2 2 2 2 2 2
987654321
1 2 3 4 5 6 7 8 9

21
6
45

Hint

Sample 1 Explanation

In the first test case, the optimal operations are:

  • Delete digit 22 with a cost of 1010. After that, nn becomes 1313;
  • Delete digit 33 with a cost of 1010. After that, nn becomes 11;
  • Delete all the remaining digits of nn with a cost of 11.

The total cost is 10+10+1=2110+10+1=21. It can be shown that this is the minimum cost.

In the second test case, the optimal operations are:

  • Delete the first digit 11 with a cost of 22. After that, nn becomes 121121;
  • Delete the last digit 11 with a cost of 22. After that, nn becomes 1212;
  • Delete digit 22 with a cost of 11. After that, nn becomes 11;
  • Delete all the remaining digits of nn with a cost of 11.

The total cost is 2+2+1+1=62+2+1+1=6.

Sample 2

See bargain/bargain2.in and bargain/bargain2.ans in the attachments.

This sample satisfies the constraints of test 363\sim 6.

Sample 3

See bargain/bargain3.in and bargain/bargain3.ans in the attachments.

This sample satisfies the constraints of test 1111.

Sample 4

See bargain/bargain4.in and bargain/bargain4.ans in the attachments.

This sample satisfies the constraints of test 17,1817,18.

Sample 5

See bargain/bargain5.in and bargain/bargain5.ans in the attachments.

This sample satisfies the constraints of test 232523\sim 25.


Constraints

For all the tests, it is guaranteed that:

  • 1t101\le t\le 10;
  • 1n<101051\le n< 10^{10^5};
  • For each 1i91\le i\le 91vi1051\le v_i\le 10^5;
  • nn consists only of digits 191\sim 9.
Test Id n<n< viv_i\le Special Properties
11 100100 10510^5 -
22 10310^3
363\sim 6 101810^{18}
797\sim 9 104010^{40}
1010 1010510^{10^5} nn consists of only one type of digits.
1111 nn consists of at most two types of digits.
12,1312,13 nn consists of at most three types of digits.
141614\sim 16 1010310^{10^3} v1=v2=v3==v9v_1=v_2=v_3=\dots =v_9
17,1817,18 1010510^{10^5}
19,2019,20 1010010^{100} 100100 -
21,2221,22 1010310^{10^3} 10310^3
232523\sim 25 1010510^{10^5} 10510^5