#P9688. Colo.

    ID: 10945 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP洛谷原创O2优化背包 DP洛谷月赛

Colo.

Problem Description

Little F and Little Y often play together. Since Little F is a painter, he likes to draw on a grid of length nn and width 11. The ii-th cell from left to right is painted with a color aia_i.

You think his random doodles look too ugly, so you want to keep exactly kk colors (you are not allowed to keep colors that do not appear in the grid). All cells that are not painted with any color you keep will be cut off, and some cells will remain in the end. You want the color indices of the remaining cells, from left to right, to be monotonically non-decreasing.

In addition, the ii-th color used by Little Y has a value bib_i. Little Y is very happy after seeing the grid you cropped, so he decides to pay you the sum of the values of the colors you chose.

You need to find the maximum total value you can obtain.

Input Format

The first line contains two integers n,kn,k, representing the size of the grid Little Y drew and the number of colors you need to keep.
The second line contains nn integers aia_i, representing the color of the ii-th cell from left to right in the grid drawn by Little Y.
The third line contains nn integers bib_i, representing the value of the ii-th color.

Output Format

Output one integer, representing the maximum value you can obtain. In particular, if it is impossible to find a way to choose colors that satisfies the requirements, output 1-1.

5 2
1 2 1 3 2
5 3 1 100 100
6
10 3
1 3 4 2 9 3 4 2 5 1
1 5 2 3 9 8 1 2 3 10
-1

Hint

Sample Explanation #1

For the first sample, we can choose to keep colors 11 and 33. The remaining grid is [1,1,3][1,1,3], which satisfies the monotonically non-decreasing constraint. The value obtained is b1+b3=5+1=6b_1+b_3=5+1=6, and it can be proven that this is optimal.

Constraints

For all testdata, 1n5001 \le n \le 500, 1k5001 \le k \le 500, 1ain1 \le a_i \le n, 1bi1091 \le b_i \le 10^9.

This problem uses bundled tests. All test points with the same constraints are bundled into one Subtask\text{Subtask}.

The additional constraints of each test point are shown in the table below.

Test Point n,kn,k \le Special Property
131 \sim 3 1010 None
454 \sim 5 100100
6106 \sim 10 500500 The number of distinct colors does not exceed 1010
111511 \sim 15 Each color appears at most 22 times
162016 \sim 20 None

Translated by ChatGPT 5