#K43027. Minimize Total Arrangement Width
Minimize Total Arrangement Width
Minimize Total Arrangement Width
You are given n books where each book has two attributes: its width and its height. In addition, a specific sequence of heights is provided which represents the required order in which the books must be arranged. The task is to compute the minimum total width of the arrangement such that the heights of the books, when arranged according to the sequence, match the given order.
More formally, let each book be represented as a tuple \((w, h)\) where w is the width and h is the height. Given a list of books and a height sequence \(H = [h_1, h_2, \dots, h_n]\), you need to determine the sum of the widths corresponding to the books arranged in the specified height order. In mathematical terms, if the mapping from height to width is one-to-one, compute:
[ \text{TotalWidth} = \sum_{i=1}^{n} w_i \quad \text{such that the book with height } h_i \text{ has width } w_i. ]
It is guaranteed that the mapping is unique and that each height in the sequence corresponds exactly to one of the provided books.
inputFormat
The input is read from standard input and has the following format:
- An integer
n
representing the number of books. n
lines follow, each containing two integers separated by space: the width and the height of a book.- A single line containing
n
integers specifying the required height arrangement sequence.
For example:
3 10 20 5 30 15 10 30 20 10
outputFormat
Output a single integer to standard output which is the minimum total width of the valid arrangement.
For the above sample input, the output is:
30## sample
3
10 20
5 30
15 10
30 20 10
30