E. Selling Numbers

Codeforces
IDCF778E
Time2000ms
Memory512MB
Difficulty
dpsortings
English · Original
Chinese · Translation
Formal · Original
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. Recently 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. What is the maximum cost of the collection Boris can achieve after using the artifact? ## Input First 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_'. Last line contains ten integers — costs of digits _c_0, _c_1, ..., _c_9 (0 ≤ _c__i_ ≤ 1000). ## Output Output one integer — the maximum possible cost of the collection after using the artifact. [samples] ## Note In 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.
Boris 非常喜欢数字,甚至经营一家出售有趣数字的小店。他拥有 #cf_span[n] 个十进制数 #cf_span[Bi]。他商店中一个数字的价格等于其各位数字的成本之和。给你每个数字 #cf_span[d] 的成本值 #cf_span[cd]。当然,Boris 希望他拥有的数字具有尽可能高的总成本。 最近,Boris 获得了魔法神器 #cf_span[A],它能让他提升自己收藏的总成本。该神器是一个由数字和 '_?_' 字符组成的字符串。要使用该神器,Boris 必须将所有 '_?_' 替换为数字,得到一个没有前导零的十进制数(也不允许得到数字 0)。之后,将得到的数字加到 Boris 收藏的每个数字 #cf_span[Bi] 上。他恰好使用一次该神器。 Boris 在使用神器后,他的收藏能达到的最大成本是多少? 第一行包含神器 #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])。 输出一个整数 —— 使用神器后,收藏能达到的最大可能成本。 在第二个样例输入中,最优方案是组成数字 _453_。添加这个数字后,Boris 将拥有数字 _2656_、_5682_、_729_ 和 _6696_。它们所有数字的总成本为 #cf_span[18 + 15 + 11 + 18 = 62]。 ## Input 第一行包含神器 #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])。 ## Output 输出一个整数 —— 使用神器后,收藏能达到的最大可能成本。 [samples] ## Note 在第二个样例输入中,最优方案是组成数字 _453_。添加这个数字后,Boris 将拥有数字 _2656_、_5682_、_729_ 和 _6696_。它们所有数字的总成本为 #cf_span[18 + 15 + 11 + 18 = 62]。
**Definitions** Let $ A $ be a string of length $ m $, consisting of digits $ \{0,1,\dots,9\} $ and wildcard symbols $ \texttt{?} $. Let $ n \in \mathbb{Z}^+ $ be the number of integers in Boris’s collection. Let $ 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 $. Let $ 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 $. Let $ X $ be the integer formed by replacing each $ \texttt{?} $ in $ A $ with a digit from $ \{0,1,\dots,9\} $, such that: - $ X $ has no leading zeros (i.e., if $ |A| > 1 $, the first digit of $ X $ cannot be $ 0 $), - $ X \neq 0 $. Let $ B_i' = B_i + X $ for each $ i \in \{1, \dots, n\} $, interpreted as decimal string addition. Let $ \text{cost}(S) $ for a decimal string $ S $ be the sum of costs of its digits: $$ \text{cost}(S) = \sum_{d \in \text{digits}(S)} c_d $$ **Constraints** 1. $ 1 \leq |A| \leq 1000 $ 2. $ 1 \leq n \leq 1000 $ 3. $ 1 \leq |B_i| < 1001 $ for all $ i $ 4. $ 0 \leq c_d \leq 1000 $ for all $ d \in \{0, \dots, 9\} $ 5. $ A $ does not start with '0' 6. $ X \neq 0 $ and has no leading zeros **Objective** Maximize the total cost of the updated collection: $$ \max_{X} \sum_{i=1}^{n} \text{cost}(B_i + X) $$ where the maximum is taken over all valid $ X $ formed by replacing $ \texttt{?} $ in $ A $ with digits under the constraints above.
Samples
Input #1
42
3
89
1
958
0 0 1 1 2 2 3 3 4 4
Output #1
4
Input #2
?5?
4
2203
5229
276
6243
2 1 6 1 1 2 5 2 2 3
Output #2
62
API Response (JSON)
{
  "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...",
      "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],它能让他提升自己收藏的总成本。该...",
      "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...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments