Two-Area Database
InterviewTime limit1sMemory limit256 MB
Read a fixed sequence of data kinds using a one-slot cache that loads at cost c and serves hits for free, and minimize total read cost.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
A database keeps its data in two areas. Area0 holds every kind of data. Area1 holds exactly one kind at a time.
Reading the -th kind of data from Area0 costs . Reading the data currently held in Area1 costs nothing.
You may pick any one kind of data in Area0 and copy it into Area1. The copy costs whichever kind you pick, and it erases whatever Area1 held before. You may copy at any moment and as many times as you want, and a copy leaves Area0 unchanged.
The kinds of data to read today and their order are fixed in advance and are all given to you. Area1 starts empty.
Find the smallest total cost of reading all of the data in the given order.
Input
The first line contains the number of test cases .
The first line of each test case contains three integers separated by spaces: the number of reads to perform today (), the number of kinds of data (), and the cost () of copying one piece of data from Area0 into Area1.
The second line contains integers () separated by spaces. is the cost of reading the -th kind of data from Area0.
The third line contains integers () separated by spaces. is the kind of data that must be read -th.
At the start every kind of data is stored in Area0 and Area1 is empty.
Output
For each test case, print the minimum cost of reading all of the data in order, one answer per line.