#P3701. 主主树
主主树
Description
Soon, the trees blossomed and bore fruit. byx and Shino-chan were surprised to find that theirs were Zhuzhu Trees, full of Zhuzhu and Zhuzhu’s friends. There are five kinds of people on the tree: Zhuzhu (), Jiji (), Gaogao (), Wangwang (), and Waiwai (). They found that the numbers of people on their Zhuzhu Trees are the same, both equal to .

Research shows that the win–lose relationships among the five kinds are as in the figure above (two people of the same kind cannot “PK”). Arrows point to the loser. As for why, think about it yourself.
The contest begins as scheduled.
byx and Shino-chan will play matches. In each match, they will each choose one person from their own tree to see who is stronger.
The -th person has a lifetime of seconds. After each match, both participants lose . When someone’s lifetime reaches , they can no longer play.
Also, when a ’s lifetime is , any on the same tree can add to that . Each can extend each at most once.
The problem:
Given , the kind of each person for Shino-chan and byx (one of $\verb!J!, \verb!HK!, \verb!W!, \verb!YYY!, \verb!E!$), and each person’s lifetime (not exceeding ), compute the maximum number of matches that byx can win.
The testdata guarantees that there will be available participants for every match. Any pair of two specific people can play at most one match.
Input Format
The first line contains two integers , as described above.
The second line contains strings ( or ), the kinds of byx’s people, separated by spaces.
The third line contains strings ( or ), the kinds of Shino-chan’s people, separated by spaces.
The fourth line contains integers, the lifetimes of byx’s people.
The fifth line contains integers, the lifetimes of Shino-chan’s people.
Output Format
Output a single integer: the number of matches byx can win.
3 3
J W YYY
J HK E
2 2 2
2 2 2
3
Hint
In the first match, beats . In the second match, beats . In the third match, beats .
Translated by ChatGPT 5
京公网安备 11011102002149号