Bunny, the Jumper
Practice
5 (2 votes)
Dynamic programming
Algorithms
Applications of dynamic programming
Problem
44% Success 159 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You're in an imaginary city where Bugs Bunny, the rabbit lives. Bugs needs to navigate through a row of \(N\) cells in the city, where each cell can either be a rabbit hole (Bugs can enter it) or an obstacle that he can't jump into.

Your mission is to guide Bugs through the city successfully. Bugs starts by hopping into the \(x\)\(th \) cell from the beginning, then jumps to \(x + y^{\text{th}}\) cell, then jumps to \(x + 2*y^{\text{th}}\) cell and so on until he reaches a cell beyond the last one. If any of these cells is an obstacle, Bugs can't proceed.

To help Bugs on his adventure, you can modify the city layout with two actions:

  • Spend \(A[i]\) coins to turn an obstacle cell into a rabbit hole
  • Spend \(B\) coins to remove the first cell entirely and thus changing the number of cells, but you must maintain a minimum of \(x\) cells in the city.

Your challenge is to calculate the minimum cost in coins required to modify the city layout, ensuring that Bugs Bunny can follow the given \(x\) and \(y\) pattern.

Input Format:

  •  First line of the input contains an integer \(T\), representing the number of test cases.
  • The first line of each test case contains an integer \(N\) representing the number of cells in the city.
  • The next line of each test case contains \(3\) integers \(x\)\(y\) and \(B\) as given in the question.
  • The next line of each test case contains \(N \) integers separated by space containting either \(0\) or \(1\), indicating if \(i^{th}\) city contains an obstacle or a rabbit hole respectively.
  • The next line of each test case contains \(N\) integers separated by space representing the array \(A\).

Output Format:

  • For each test case, output a single integer representing the minimum coins required.

Constraints

  • \(1 \leq T \leq 10^5 \)
  • \(1 \leq N \leq 10^5\)
  • \(1 \leq A[i], B \leq 10^9 \)
  • \(1 \leq x, y \leq N \)
  • It is guaranteed that sum of \(N\) over all test cases does not exceed \(10^6\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
Medium
Points:30
3 votes
Tags:
Medium
Points:30
Tags:
Dynamic ProgrammingRecruitMediumAlgorithmsReadyApproved