| OCPC 2025 Summer, Day 8: Chromate Sent This to Dominater in His DMs and Dominater Said :---) Contest |
|---|
| Finished |
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.
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$$$.
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 $$$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.
6 6tomatopotato1 1 1 1 1 11 1 1 1 1 112
4 4 3 3 3 3 3 3 3 3 2 2
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:
Note that two distinct common subsequences with content $$$\mathtt{oto}$$$ appear, as one of the two sequences differs in the two common subsequences.
![]() | ![]() |
| Name |
|---|


