D. Weighted Common Subsequences
time limit per test
7 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Azora and Quark have strings $$$S$$$ and $$$T$$$, with length $$$n$$$ and $$$m$$$ respectively.

A common subsequence of $$$S$$$ and $$$T$$$ with length $$$l$$$ is defined as a pair of integer sequences $$$(p,q)$$$ that satisfy the following conditions.

  • $$$|p|=|q|=l$$$;
  • $$$1 \le p_1 \lt p_2 \lt \ldots \lt p_{l-1} \lt p_l \le |S|$$$;
  • $$$1 \le q_1 \lt q_2 \lt \ldots \lt q_{l-1} \lt q_l \le |T|$$$;
  • $$$S_{p_i}=T_{q_i}$$$ for all $$$1 \le i \le l$$$.

Additionally, you are given two functions $$$f: (\mathbb{N} \cap [1,n]) \to \mathbb{N}$$$ and $$$g: (\mathbb{N} \cap [1,m]) \to \mathbb{N}$$$.

Then, the weight of the common subsequence $$$(p,q)$$$ with length $$$l$$$ is defined as follows.

$$$$$$\sum_{i=1}^l{f(p_i) \cdot g(q_i)}$$$$$$

Note that even if the contents of two common subsequences are the same, they are considered different if either of the two integer sequences differs. For example, for $$$S=\mathtt{aba}$$$ and $$$T=\mathtt{aaa}$$$, $$$([1,3],[1,2])$$$ and $$$([1,3],[2,3])$$$ are considered different common subsequences while the content is $$$\mathtt{aa}$$$ for both. Also, $$$([\,],[\,])$$$ is considered a common subsequence of any two strings.

Given an integer $$$k$$$, please find the weights of the $$$k$$$ maximum weight common subsequences of $$$S$$$ and $$$T$$$.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ — the lengths of the strings $$$S$$$ and $$$T$$$ ($$$1 \le n,m \le 1000$$$).

The second line contains the string $$$S$$$.

The third line contains the string $$$T$$$.

The fourth line contains $$$n$$$ integers $$$f(1),f(2),\ldots,f(n)$$$ ($$$1 \le f(i) \le 1000$$$).

The fifth line contains $$$m$$$ integers $$$g(1),g(2),\ldots,g(m)$$$ ($$$1 \le g(i) \le 1000$$$).

The sixth line contains a single integer $$$k$$$ — the number of common sequences to find ($$$1 \le k \le 2\cdot 10^6$$$).

It is guaranteed that both $$$S$$$ and $$$T$$$ consist of lowercase Latin letters.

Output

Output $$$k$$$ lines. On the $$$i$$$-th line, output the weight of the $$$i$$$-th maximum weight common subsequence.

If the number of distinct common subsequences is strictly less than $$$i$$$, output $$$-1$$$ on the $$$i$$$-th line instead.

Note that there exists a common subsequence of length $$$0$$$ also.

Example
Input
6 6
tomato
potato
1 1 1 1 1 1
1 1 1 1 1 1
12
Output
4
4
3
3
3
3
3
3
3
3
2
2
Note

In the example, the weight of the common subsequences is equivalent to the length of the common subsequence.

The $$$12$$$ maximum weight common subsequences of $$$\mathtt{tomato}$$$ and $$$\mathtt{potato}$$$ are as follows:

  • $$$([1,4,5,6],[3,4,5,6])$$$: $$$\mathtt{tato}$$$
  • $$$([2,4,5,6],[2,4,5,6])$$$: $$$\mathtt{oato}$$$
  • $$$([1,5,6],[3,5,6])$$$: $$$\mathtt{tto}$$$
  • $$$([2,5,6],[2,3,6])$$$: $$$\mathtt{oto}$$$
  • $$$([2,5,6],[2,5,6])$$$: $$$\mathtt{oto}$$$
  • $$$([4,5,6],[4,5,6])$$$: $$$\mathtt{ato}$$$
  • $$$([1,4,5],[3,4,5])$$$: $$$\mathtt{tat}$$$
  • $$$([2,4,5],[2,4,5])$$$: $$$\mathtt{oat}$$$
  • $$$([1,4,6],[3,4,6])$$$: $$$\mathtt{tao}$$$
  • $$$([2,4,6],[2,4,6])$$$: $$$\mathtt{oao}$$$
  • $$$([1,4],[3,4])$$$: $$$\mathtt{ta}$$$
  • $$$([2,4],[2,4])$$$: $$$\mathtt{oa}$$$

Note that two distinct common subsequences with content $$$\mathtt{oto}$$$ appear, as one of the two sequences differs in the two common subsequences.

This area is not intentionally blank. Well, regardless, please enjoy the drawings of Azora and Quark. Also do help Azora and Quark solve the problem on the previous few pages; neither was able to solve it.