Break the adjacency
Practice
3.5 (2 votes)
2d dynamic programming
Algorithms
Dynamic programming
Problem
39% Success 272 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alex on his walk to Forest of leaf discovers \(N\) sticks lying in front of him. Each stick has two values represented by two arrays \(arr\) and \(price\) of size \(N\) where in array \(arr\), \(arr[i]\) \((0 <= i <= N-1)\) denotes length of \(i'th\) stick and array \(price\), \(price[i]\) \((0 <= i <= N-1)\) denotes price to increment length of \(i'th\) stick by \(1\) unit.

Alex calls an array Broken Adjacency when none of the adjacent sticks has equal length. A stick \(i\) is adjacent to stick \(i-1\) (if any) and stick \(i+1\) (if any). He wants to find the minimum cost to achieve Broken Adjacency. To do so, in 1 move he can increment the length of any \(i'th\) stick by \(1\) unit at the price of \(price[i]\).

Can you help him find the minimum cost to achieve his goal?

Input Format:

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first input line of each test case contains one integer \(N\), denoting the number of sticks found.
  • The second line contains \(N\) space-separated integers denoting the elements of array \(arr\).
  • The third line contains \(N\) space-separated integers denoting the elements of array \(price\).

Output Format:

Print the minimum cost to make given sticks to Broken Adjacent sticks.

Constraints:

\(1 <= T <= 10\)

\(1 <= N <= 10^5\)

\(1 <= arr[i] <= 10^9\)

\(1 <= price[i] <= 10^9\)

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:50
44 votes
Tags:
ApprovedEuler's totient functionMathMediumOpenReadySieve
Points:50
10 votes
Tags:
AlgorithmsDynamic Programming2D dynamic programmingC++
Points:50
1 votes
Tags:
2D dynamic programmingDynamic ProgrammingAlgorithms