휴가 나가기
InterviewTime limit1sMemory limit1024 MB
Given tasks with importance, processing time, and at most one prerequisite each, find the minimum total time to reach importance S.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Problem
Yongbeom wants to finish his backlog of tasks before he leaves for vacation. There is not enough time to finish every task, so he will complete only the important ones and then leave.
There are pending tasks, numbered from 1 to . Each task may have at most one prerequisite task, and no two tasks share the same prerequisite. Yongbeom can work on only one task at a time. Each task has an importance and a processing time . The time spent on a set of tasks is the sum of their processing times.
If there were enough time, Yongbeom could finish every task. Find the minimum time needed for the completed tasks to reach a total importance of at least .
Input
The first line contains two integers and , separated by a space. ,
The second line contains the importance of each task, separated by spaces.
The third line contains the processing time of each task, separated by spaces.
The fourth line contains the prerequisite task number of each task, separated by spaces. If is 0, task has no prerequisite. All nonzero values are distinct.
The input is given only when every task can be completed if enough time is available.
Output
Print the minimum time needed for the total importance of the completed tasks to be at least . If the total importance of all tasks is less than , print .