아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Boxes and Balls

시간 제한1초메모리 제한512 MB

요약
M개의 상자에 공을 담는데, 요청된 공이 상자에 없으면 w를 지불하고 상자 하나에서 공을 빼내야 한다. 총비용의 최솟값을 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 비트 연산, 그리디
정답자
아직 제출이 없습니다

문제

There are MM boxes and NN balls. The balls are numbered 11 through NN, and the weight of the ball ii is w_iw\_i. You are also given a sequence a_1,a_2,…,a_Ka\_1, a\_2, \ldots, a\_K. Each a_ja\_j is an integer satisfying 1≤a_j≤N1 \le a\_j \le N.

Initially, all the boxes are empty. For each j=1,2,…,Kj = 1, 2, \ldots, K in this order, you have to perform the following operation:

  • If one of the boxes contains the ball a_ja\_j, you do nothing. There is no cost for this operation.
  • Otherwise, you choose one of the boxes and put the ball a_ja\_j into the chosen box. However, if the chosen box already contains another ball, you should take that ball out of the box. The cost for this operation is w_a_jw\_{a\_j} (the cost doesn't depend on the box nor the ball you take out of the box).

Compute the minimum possible total cost of operations.

입력

The first line contains three integers MM, NN and KK (1≤M≤101 \le M \le 10, 1≤N,K≤1041 \le N, K \le 10^4).

The ii-th of the next NN lines contains an integer w_iw\_i (1≤w_i≤1041 \le w\_i \le 10^4).

The jj-th of the next KK lines contains an integer a_ja\_j (1≤a_j≤N1 \le a\_j \le N).

출력

Print the minimum total cost.

예제2

  1. 예제 1

    입력
    3 3 6
    10
    20
    30
    1
    2
    3
    1
    2
    3
    
    예상 출력
    60
    
  2. 예제 2

    입력
    2 3 6
    10
    20
    30
    1
    2
    3
    1
    2
    3
    
    예상 출력
    80