For Programming Excellence
Time limit8sMemory limit512 MB
Given a prerequisite tree of skills with level caps and importance weights, find the maximum sum of level times importance within a tuition budget.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Problem
A programmer needs countless skills to become excellent. Each skill has its own importance degree, and the total programming competence is the sum of the products of each skill's level and its importance degree.
This summer, you plan to attend a summer programming school. The school offers courses for many of these skills. When you take a course for a skill, its level rises in proportion to the tuition you pay, one level per one yen. Each skill has an upper limit on its level, and spending more on it does not raise the level past that limit. Skills depend on one another. Except for the most basic course, a course for a skill requires that its prerequisite skill be at least a certain level.
You want the highest possible programming competence within your tuition budget.
Input
The input consists of no more than 100 datasets, each in the following format.
n k
h1 ... hn
s1 ... sn
p2 ... pn
l2 ... ln
The first line has two integers, , the number of skills (), and , the tuition budget (). Skills are numbered 1 through .
The second line has integers . is the maximum level of skill , between 1 and inclusive.
The third line has integers . is the importance degree of skill , between 1 and inclusive.
The fourth line has integers . is the prerequisite skill of skill , between 1 and inclusive. Skill 1 has no prerequisite.
The fifth line has integers . is the least level of prerequisite skill required to learn skill , between 1 and inclusive.
The end of the input is a line containing two zeros.
Output
For each dataset, output one line containing a single integer: the highest programming competence measure achievable, that is, the maximum sum of the products of levels and importance degrees of the skills, within the given tuition budget. All skills start at level zero. You do not have to use up the whole budget.