Codeforces Round 968 (Div. 2)
F
Turtle and Three Sequences
Codeforces
2800
原题
← E2→
翻译中…

Piggy gives Turtle three sequences $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$, and $c_1, c_2, \ldots, c_n$.

翻译中…

Turtle will choose a subsequence of $1, 2, \ldots, n$ of length $m$, let it be $p_1, p_2, \ldots, p_m$. The subsequence should satisfy the following conditions:

翻译中…
  • $a_{p_1} \le a_{p_2} \le \cdots \le a_{p_m}$;
  • All $b_{p_i}$ for all indices $i$ are pairwise distinct, i.e., there don't exist two different indices $i$, $j$ such that $b_{p_i} = b_{p_j}$.
翻译中…

Help him find the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$, or tell him that it is impossible to choose a subsequence of length $m$ that satisfies the conditions above.

翻译中…

Recall that a sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements.

翻译中…

Input

翻译中…

The first line contains two integers $n$ and $m$ ($1 \le n \le 3000$, $1 \le m \le 5$) — the lengths of the three sequences and the required length of the subsequence.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of the sequence $a$.

翻译中…

The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$) — the elements of the sequence $b$.

翻译中…

The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^4$) — the elements of the sequence $c$.

翻译中…

Output

翻译中…

Output a single integer — the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$. If it is impossible to choose a subsequence of length $m$ that satisfies the conditions above, output $-1$.

翻译中…

Note

翻译中…

In the first example, we can choose $p = [1, 2]$, then $c_{p_1} + c_{p_2} = 1 + 4 = 5$. We can't choose $p = [2, 4]$ since $a_2 \gt a_4$, violating the first condition. We can't choose $p = [2, 3]$ either since $b_2 = b_3$, violating the second condition. We can choose $p = [1, 4]$, but $c_1 + c_4 = 4$, which isn't maximum.

翻译中…

In the second example, we can choose $p = [4, 6, 7]$.

翻译中…

In the third example, it is impossible to choose a subsequence of length $3$ that satisfies both of the conditions.

翻译中…

样例

Input #1
4 2
2 3 4 2
1 3 3 2
1 4 2 3
Output #1
5
InputCopy OutputCopy
Input #2
7 3
1 4 5 2 3 6 7
1 2 2 1 1 3 2
1 5 6 7 3 2 4
Output #2
13
InputCopy OutputCopy
Input #3
5 3
1 2 3 4 5
1 1 2 1 2
5 4 3 2 1
Output #3
-1
InputCopy OutputCopy
题目信息
题号
2003F
时间限制
3 s
内存限制
256 MB
标签
暴力
数据结构
动态规划
贪心
数学
概率论
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。