#P11188. Minimum Deletion Cost
Minimum Deletion Cost
Minimum Deletion Cost
You are given a positive integer \(n\) which is guaranteed to contain only the digits from \(1\) to \(9\). In addition, for every digit \(x\) (\(1 \le x \le 9\)) there is an associated deletion cost \(v_x\).
You can perform the following operations any number of times:
- Choose one digit \(x\) in \(n\) and delete it at the cost of \(v_x\). Note that after deletion, both the number of digits and the numerical value of \(n\) will change accordingly.
- Pay a cost equal to the current value of \(n\) (when interpreted as an integer) to delete all of its remaining digits simultaneously.
Your task is to determine the minimum cost required to delete the entire number.
The operations can be performed in any order. If the current number is represented by the string \(s\), then the cost to delete it entirely in one operation is \(\texttt{value}(s)\), where \(\texttt{value}(s)\) denotes the integer value of the string \(s\). Alternatively, for each digit in \(s\), you can remove that digit at a cost of \(v_x\) (if the digit is \(x\)) and then solve the problem recursively on the new number obtained by removing that digit. Formally, if \(s\) is non-empty, let
\[ f(s)=\min\Bigg\{\texttt{value}(s), \min_{i}\Big\{v_{s[i]}+f(s\setminus\{s[i]\})\Big\}\Bigg\}, \]with the base condition \(f(\texttt{\"\"})=0\). Compute \(f(s)\) for the given number \(s\).
inputFormat
The input consists of two lines:
- The first line contains the positive integer \(n\) (as a string of digits from 1 to 9).
- The second line contains 9 space-separated integers \(v_1, v_2, \ldots, v_9\), where \(v_x\) is the cost to delete a digit \(x\).
outputFormat
Output a single integer, representing the minimum cost required to delete the entire number.
sample
123
1 2 3 4 5 6 7 8 9
6