

A 1-day pass is sold for 'COST'[0] coins,
A 7-day pass is sold for 'COST'[1] coins, and
A 30-day pass is sold for 'COST'[2] coins.
The passes allow for many days of consecutive travel.
If Ninja gets a 7-day pass on day 2, then he can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, 'N,’ denoting the number of days Ninja wants to travel.
The second line of each test case has ‘N’ sorted integers corresponding to the DAYS.
The third line of each test case contains a 'COST' array having three integers corresponding to the 'COST' of a 1-day pass, a 7-day pass, and a 30-day pass.
For each test case, print a single integer corresponding to the minimum number of coins required to travel every day in the given list of days
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 365
1 <= DAYS[i] <= 365
Time Limit: 1 sec
In this approach, we will traverse each day recursively and if we are not traveling on that day, we will wait to buy a pass. Otherwise, we have three choices to buy a pass ( a 1-day, 7-day, or 30-day pass).
We can express those choices as recursion and use dynamic programming. Let's say 'DP'[i] is the 'COST' to fulfill your travel plan from day ‘i’ to the end of the plan. Then, if you have to travel today, your 'COST' is:
'DP'[i] = min('DP'[i+1] + 'COST'[0], 'DP'[i+7] + 'COST'[1], 'DP'[i+30] + 'COST'[2])
And if ith day is not a 'TRAVELDAY':
'DP'[i] = 'DP'[i+1]
We will an array 'TRAVELDAY' to check whether the ith day is travel day or not.
In this approach, we will traverse each day recursively and if we are not traveling on that day, we will wait to buy a pass. Otherwise, we have three choices to buy a pass ( a 1-day, 7-day, or 30-day pass).
We can express those choices as recursion and use dynamic programming. Let's say 'DP'[i] is the 'COST' to fulfill your travel plan from day ‘i’ to the end of the plan. Then, if you have to travel today, your 'COST' is:
'DP'[i] = min('DP'[i+1] + 'COST'[0], 'DP'[i+7] + 'COST'[1], 'DP'[i+30] + 'COST'[2])
And if ith day is not a 'TRAVELDAY':
'DP'[i] = 'DP'[i+1]
We will use two arrays one 'TRAVELDAY' to check whether the ith day is travel day or not and 'MEMO' for memoization of this recursion.
In approach 1, we are traversing through all the days of the year and calculating the minimum coins required. In this approach, we will just traverse through the given days on which Ninja is traveling.
Let 'DP'[i] denotes the minimum cost from the day 'DAYS'[i] to the end of the trip.Let ‘j1’ should be the smallest index such that 'DAYS'[j1] >= 'DAYS'[i] +1.
Simlarly ‘j7’ and ‘j30’ is the smallest index such that 'DAYS'[j7]>='DAYS'[i]+7 and 'DAYS'[j30]>='DAYS'[i]+30.Then
'DP'[i] = min('DP'[j1] +'COST'[0], 'DP'[j7] +'COST'[1], 'DP'[j30] +'COST'[2]).
In the previous approach, we will just traverse through the given days on which Ninja is traveling and storing them in a DP table.
In this approach, we will create two queues named ‘WEEK’ and ‘MONTH’.These queues will store pairs that will denote {day, cost till that day}. Then we will iterate over all traveldays and insert them into the queue and also remove elements from these queues if the pass expires. The cheapest pass price will always be the front element of the queue.