P18092 [CCPC 2026 Changchun] A Different Course Selection Battle
Background
O5 is an undergraduate student at University T, and he is worrying about course selection for this semester.
Description
A week at University T has $n$ days, and each day has $m$ class periods. O5's training program forms a rooted tree with $W+H+1$ nodes, where:
- Node $0$ is the root node, representing the whole training program;
- Nodes numbered from $1$ to $W$ are non-leaf, non-root nodes representing course modules, and each course module has a concreteness $q_i$;
- Nodes numbered from $W+1$ to $W+H$ are leaf nodes representing specific courses. The $i$-th course corresponds to the node numbered $W+i$, which has $l_i$ available class periods $(d_{i,j},t_{i,j})$, meaning that the class is held in the $t_{i,j}$-th period of the $d_{i,j}$-th day of each week. Each course only needs to be taken in one period.
Since modules further down are more concrete, **for any non-leaf, non-root node $a\;(1\leq a\leq W)$, we have $q_a\geq q_{fa_a}$**, where $fa_a$ denotes the parent node of $a$, and $q_0=0$ is specified. Also, University T does not allow overlapping classes, that is, at most one course may be taken in the same period.
For any two different courses, let the nodes corresponding to them on the tree be $i,j$; then the similarity of these two courses is defined as $q_{lca(i,j)}$, where $lca(i,j)$ denotes the lowest common ancestor of $i$ and $j$.
O5 needs to choose exactly $K$ courses, and he wants to minimize the sum of the similarities over all pairs of chosen courses, that is: letting the chosen courses correspond to the nodes $c_1,\cdots,c_K$ on the tree, find the minimum value of $\sum_{1\leq i
Input Format
The first line contains five positive integers $n,m,W,H,K$ $(1\le n,m,W,H\le 10^4,nm\le 10^4, 2\le K \le 10^3)$, whose meanings are given in the statement.
The second line contains $W+H$ integers $fa_1,\cdots,fa_{W+H}$ $(0\le fa_i\le W)$, the parent nodes of the nodes.
The third line contains $W$ integers $q_1,\cdots,q_W$ $(0\le q_i\le 10^9)$, the concreteness of the course modules. **It is guaranteed that $q_{i}\ge q_{fa_i}$ for $1\le i \le W$.**
The next $H$ lines describe the courses. The $i$-th line first contains a positive integer $l_i$ $(1\le l_i\le 10^4)$, the number of available class periods of the $i$-th course; then it contains $2l_i$ positive integers $d_{i,1},t_{i,1},d_{i,2},t_{i,2},\cdots,d_{i,l_i},t_{i,l_i}$ $(1\le d_{i,j}\le n,1\le t_{i,j}\le m)$, the class periods available to the $i$-th course.
It is guaranteed that the given course nodes form a tree, and that the sum of $l_i$ over all courses does not exceed $10^4$.
Output Format
If it is impossible to choose exactly $K$ courses without time conflicts, output `-1`.
Otherwise, output a non-negative integer, the minimum possible sum of the similarities over all pairs of courses when $K$ courses are chosen.
Explanation/Hint
For sample 1, one optimal plan is to choose courses 1 and 3 and place them in period 1 of day 2 and period 1 of day 1 respectively. The lowest common ancestor of the two courses is the root node, so the similarity is $0$. Choosing courses 2 and 3, or 1 and 4, also achieves the optimum.
For sample 2, one should choose courses 1, 2, 4 and place them in period 1 of day 1, period 2 of day 1, and period 1 of day 2 respectively. The similarity of courses 1 and 2 is 5, and the similarities of courses 1 and 4, and of courses 2 and 4, are both $2$, for a total of $9$.
For sample 3, courses 1 and 2 always conflict, so it is impossible to choose 3 courses, hence there is no solution.
Translated by QwenWork