Yukhoe Bibimbap
InterviewTime limit1sMemory limit512 MB
Bosun must visit all N stations in some order where consecutive glare products stay at most K, maximizing the sum of A of the previous station times B of the next.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Bosun went to Jinju for a trip and was so hungry that he went to Daean-dong in Jinju to eat yukhoe bibimbap. Lucky him, a tasting event for yukhoe bibimbap was underway. Bosun, a big eater and a gourmet, wants to eat every bowl to get the most umami possible. But the staff at the tasting stations have started to glare at him, even though he is shameless. So he decided to study the information about each station and eat strategically.
There are tasting stations in total. Each station has one bowl of yukhoe bibimbap and one staff member. The sweetness and saltiness of the bibimbap, and how much the staff member glares, can be expressed as numbers. Bosun can pick any station that still has bibimbap left, and every time he visits a station he must eat all the bibimbap there.
Let and be the sweetness and saltiness of the bibimbap at the station Bosun visits -th. The total umami he feels is:
Bosun receives no glare at the first station, but from the second station on, he is glared at. Let be the glare level of the staff at the -th station visited. Then the glare Bosun receives at the -th station () is . However, Bosun has units of shamelessness, so if the glare is or less, he ignores it and eats the bibimbap there. If the glare exceeds , he cannot eat the bibimbap there and must stop.
Considering these conditions, find the maximum total umami Bosun can feel when he eats all bowls.
Input
The first line contains the number of tasting stations and Bosun's shamelessness , separated by a space. (, )
The second line contains the sweetness of the bibimbap at each station, separated by spaces. ()
The third line contains the saltiness of the bibimbap at each station, separated by spaces. ()
The fourth line contains the glare level of the staff at each station, separated by spaces. ()
All numbers in the input are integers.
Output
On the first line, print the maximum total umami Bosun can feel when he eats all bowls. If he cannot eat all bowls of bibimbap, print -1.
Hint
Python 3 users are recommended to submit with PyPy3.