{"problem":{"name":"E. Selling Numbers","description":{"content":"Boris really likes numbers and even owns a small shop selling interesting numbers. He has _n_ decimal numbers _B__i_. Cost of the number in his shop is equal to the sum of costs of its digits. You are","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":524288},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF778E"},"statements":[{"statement_type":"Markdown","content":"Boris really likes numbers and even owns a small shop selling interesting numbers. He has _n_ decimal numbers _B__i_. Cost of the number in his shop is equal to the sum of costs of its digits. You are given the values _c__d_, where _c__d_ is the cost of the digit _d_. Of course, Boris is interested in that numbers he owns have the maximum cost possible.\n\nRecently Boris got hold of the magical artifact _A_, which can allow him to increase the cost of his collection. Artifact is a string, consisting of digits and '_?_' symbols. To use the artifact, Boris must replace all '_?_' with digits to get a decimal number without leading zeros (it is also not allowed to get number 0). After that, the resulting number is added to all numbers _B__i_ in Boris' collection. He uses the artifact exactly once.\n\nWhat is the maximum cost of the collection Boris can achieve after using the artifact?\n\n## Input\n\nFirst line contains artifact _A_, consisting of digits '_0_'–'_9_' and '_?_' symbols (1 ≤ |_A_| ≤ 1000). Next line contains _n_ — the amount of numbers in Boris' collection (1 ≤ _n_ ≤ 1000). Next _n_ lines contain integers _B__i_ (1 ≤ _B__i_ < 101000). _A_ doesn't start with '_0_'.\n\nLast line contains ten integers — costs of digits _c_0, _c_1, ..., _c_9 (0 ≤ _c__i_ ≤ 1000).\n\n## Output\n\nOutput one integer — the maximum possible cost of the collection after using the artifact.\n\n[samples]\n\n## Note\n\nIn the second sample input, the optimal way is to compose the number _453_. After adding this number, Boris will have numbers _2656_, _5682_, _729_ and _6696_. The total cost of all digits in them is equal to 18 + 15 + 11 + 18 = 62.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"Boris 非常喜欢数字，甚至经营一家出售有趣数字的小店。他拥有 #cf_span[n] 个十进制数 #cf_span[Bi]。他商店中一个数字的价格等于其各位数字的成本之和。给你每个数字 #cf_span[d] 的成本值 #cf_span[cd]。当然，Boris 希望他拥有的数字具有尽可能高的总成本。\n\n最近，Boris 获得了魔法神器 #cf_span[A]，它能让他提升自己收藏的总成本。该神器是一个由数字和 '_?_' 字符组成的字符串。要使用该神器，Boris 必须将所有 '_?_' 替换为数字，得到一个没有前导零的十进制数（也不允许得到数字 0）。之后，将得到的数字加到 Boris 收藏的每个数字 #cf_span[Bi] 上。他恰好使用一次该神器。\n\nBoris 在使用神器后，他的收藏能达到的最大成本是多少？\n\n第一行包含神器 #cf_span[A]，由数字 '_0_'–'_9_' 和 '_?_' 字符组成（#cf_span[1 ≤ |A| ≤ 1000]）。第二行包含 #cf_span[n] —— Boris 收藏中数字的个数（#cf_span[1 ≤ n ≤ 1000]）。接下来 #cf_span[n] 行包含整数 #cf_span[Bi]（#cf_span[1 ≤ Bi < 101000]）。#cf_span[A] 不以 '_0_' 开头。\n\n最后一行包含十个整数 —— 数字 #cf_span[c0, c1, ..., c9] 的成本（#cf_span[0 ≤ ci ≤ 1000]）。\n\n输出一个整数 —— 使用神器后，收藏能达到的最大可能成本。\n\n在第二个样例输入中，最优方案是组成数字 _453_。添加这个数字后，Boris 将拥有数字 _2656_、_5682_、_729_ 和 _6696_。它们所有数字的总成本为 #cf_span[18 + 15 + 11 + 18 = 62]。\n\n## Input\n\n第一行包含神器 #cf_span[A]，由数字 '_0_'–'_9_' 和 '_?_' 字符组成（#cf_span[1 ≤ |A| ≤ 1000]）。第二行包含 #cf_span[n] —— Boris 收藏中数字的个数（#cf_span[1 ≤ n ≤ 1000]）。接下来 #cf_span[n] 行包含整数 #cf_span[Bi]（#cf_span[1 ≤ Bi < 101000]）。#cf_span[A] 不以 '_0_' 开头。最后一行包含十个整数 —— 数字 #cf_span[c0, c1, ..., c9] 的成本（#cf_span[0 ≤ ci ≤ 1000]）。\n\n## Output\n\n输出一个整数 —— 使用神器后，收藏能达到的最大可能成本。\n\n[samples]\n\n## Note\n\n在第二个样例输入中，最优方案是组成数字 _453_。添加这个数字后，Boris 将拥有数字 _2656_、_5682_、_729_ 和 _6696_。它们所有数字的总成本为 #cf_span[18 + 15 + 11 + 18 = 62]。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ A $ be a string of length $ m $, consisting of digits $ \\{0,1,\\dots,9\\} $ and wildcard symbols $ \\texttt{?} $.  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of integers in Boris’s collection.  \nLet $ B = (B_1, B_2, \\dots, B_n) $ be a tuple of positive integers, each given as a decimal string with no leading zeros and $ 1 \\leq |B_i| < 1001 $.  \nLet $ c = (c_0, c_1, \\dots, c_9) \\in \\mathbb{Z}^{10} $ be the cost vector for digits $ 0 $ through $ 9 $, where $ c_d \\geq 0 $ is the cost of digit $ d $.  \n\nLet $ X $ be the integer formed by replacing each $ \\texttt{?} $ in $ A $ with a digit from $ \\{0,1,\\dots,9\\} $, such that:  \n- $ X $ has no leading zeros (i.e., if $ |A| > 1 $, the first digit of $ X $ cannot be $ 0 $),  \n- $ X \\neq 0 $.  \n\nLet $ B_i' = B_i + X $ for each $ i \\in \\{1, \\dots, n\\} $, interpreted as decimal string addition.  \n\nLet $ \\text{cost}(S) $ for a decimal string $ S $ be the sum of costs of its digits:  \n$$\n\\text{cost}(S) = \\sum_{d \\in \\text{digits}(S)} c_d\n$$\n\n**Constraints**  \n1. $ 1 \\leq |A| \\leq 1000 $  \n2. $ 1 \\leq n \\leq 1000 $  \n3. $ 1 \\leq |B_i| < 1001 $ for all $ i $  \n4. $ 0 \\leq c_d \\leq 1000 $ for all $ d \\in \\{0, \\dots, 9\\} $  \n5. $ A $ does not start with '0'  \n6. $ X \\neq 0 $ and has no leading zeros  \n\n**Objective**  \nMaximize the total cost of the updated collection:  \n$$\n\\max_{X} \\sum_{i=1}^{n} \\text{cost}(B_i + X)\n$$  \nwhere the maximum is taken over all valid $ X $ formed by replacing $ \\texttt{?} $ in $ A $ with digits under the constraints above.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF778E","tags":["dp","sortings"],"sample_group":[["42\n3\n89\n1\n958\n0 0 1 1 2 2 3 3 4 4","4"],["?5?\n4\n2203\n5229\n276\n6243\n2 1 6 1 1 2 5 2 2 3","62"]],"created_at":"2026-03-03 11:00:39"}}