REDUCTION

Given an array of integers A of size N and an integer B.

In a single operation, any B consecutive elements of A can be replaced by sum of those B elements.
Cost of one such operation is sum of those B elements.

Return the cost of reducing A into a single element, if it is not possible to reduce A in a single element return -1.



Input Format

The first argument given is the integer array A.
The second argument given is the integer B.

Output Format

Return the cost of reducing A into a single element, if it is not possible to reduce A in a single element return -1. 

Constraints

1 <= N <= 30
1 <= A[i] <= 100
2 <= B <= N

For Example

Input 1:
    A = [3, 2, 4, 1]
    B = 2
Output 1:
    20
Explaination 1:
    Operation 1: A = [5, 4, 1], Cost = 5
    Operation 2: A = [5, 5], Cost = 5
    Operation 3: A = [10], Cost = 10
    Total cost = 20
Input 1:
    A = [3, 2, 4, 1]
    B = 3
Output 1:
    -1
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving REDUCTION on Interview Code Editor
Sign Up
to access hints and editorial solutions for REDUCTION

Discussion


Loading...
Click here to start solving coding interview questions