atcoder#AGC062B. [AGC062B] Split and Insert

[AGC062B] Split and Insert

[English Translation]

Score : 700700 points

Problem Statement

There is a permutation A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) of the integers from 11 to NN. Initially, we have Ai=i (1iN)A_i=i\ (1\leq i \leq N).

Takahashi will perform the following operation on this sequence KK times.

  • Choose an integer kk such that 0k<N0 \leq k < N. Take the last kk terms of AA and insert them among the first NkN-k. More precisely, replace AA with any permutation AA' of the NN integers that satisfies the following conditions.- (A1,A2,,ANk)(A_1,A_2,\dots,A_{N-k}) is a (not necessarily contiguous) subsequence of AA'.
    • (ANk+1,ANk+2,,AN)(A_{N-k+1},A_{N-k+2},\dots,A_{N}) is a (not necessarily contiguous) subsequence of AA'.
  • (A1,A2,,ANk)(A_1,A_2,\dots,A_{N-k}) is a (not necessarily contiguous) subsequence of AA'.
  • (ANk+1,ANk+2,,AN)(A_{N-k+1},A_{N-k+2},\dots,A_{N}) is a (not necessarily contiguous) subsequence of AA'.

The cost of the series of operations is i=1KkiCi\sum_{i=1}^{K}k_iC_i, where kik_i is the kk chosen in the ii-th operation.

Takahashi wants to perform KK operations so that A=(P1,P2,,PN)A=(P_1,P_2,\dots,P_N) afterward.

Determine whether it is possible to perform such a series of operations. If it is possible, find its minimum cost.

Constraints

  • 2N1002 \leq N \leq 100
  • 1K1001 \leq K \leq 100
  • 1Ci1091 \leq C_i \leq 10^9
  • (P1,P2,,PN)(P_1,P_2,\dots,P_N) is a permutation of the integers from 11 to NN.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN KK

C1C_1 C2C_2 \dots CKC_K

P1P_1 P2P_2 \dots PNP_N

Output

If it is impossible to perform KK operations so that A=(P1,P2,,PN)A=(P_1,P_2,\dots,P_N) holds afterward, print -1. If it is possible, print the minimum cost of such a series of operations.

4 1
3
3 1 2 4
6

By choosing k=2k=2, and inserting A3=3A_3=3 before A1,A2A_1,A_2 and A4=4A_4=4 after A1,A2A_1,A_2, we can make A=(3,1,2,4)A=(3,1,2,4), which satisfies A=(P1,P2,P3,P4)A=(P_1,P_2,P_3,P_4). The cost of this operation is 2×C1=62 \times C_1 = 6.

It can be proved that the minimum cost of performing operations so that A=(P1,P2,P3,P4)A=(P_1,P_2,P_3,P_4) afterward is 66.

4 1
3
4 3 2 1
-1

It is impossible to perform operations so that A=(P1,P2,P3,P4)A=(P_1,P_2,P_3,P_4) afterward.

20 10
874735445 684260477 689935252 116941558 915603029 923404262 843759669 656978932 286318130 255195090
11 15 20 10 6 8 18 2 12 4 9 13 19 3 16 7 14 17 5 1
7372920743