Update appNew update is available. Click here to update.
Last Updated: 19 Mar, 2021
Busy Ninja
Moderate
Problem statement

Ninja is doing an offline course that has a duration of β€˜N’ days. After attending the course, the ninja gains some experience β€˜EXP[i]’ (for the day β€˜i’). On some days, the ninja is busy and will not be able to attend the course, hence not gaining any experience on that day. If β€˜BUSY[i] = 1’, then the ninja is busy on day β€˜i’. Otherwise, if β€˜BUSY[i] = 0’, the ninja attends the course on day β€˜i’. To get the most out of the course, the ninja can focus for β€˜K’ days straight (only once during the course), attending the course every day for these β€˜K’ consecutive days. You are given two arrays, β€˜EXP’ and β€˜BUSY’, each of size β€˜N’. You are also given an integer β€˜K’. The task is to find the maximum experience that the ninja can gain from this course.

Example :
β€˜N’ = 6, β€˜K’ = 3
β€˜EXP’ = [1, 4, 2, 2, 3, 6]
β€˜BUSY’ = [0, 1, 1, 0, 0, 1]
To get the maximum experience ninja has to focus on the last β€˜3’ days. The maximum experience gained will be = 1 + 2 + 3 + 6 = 12. So, the answer is 12.
Input Format :
The first line of input contains an integer β€˜T’ which denotes the number of test cases. Then, the β€˜T’ test cases follow.

The first line of each test case contains two space-integer β€˜N’ and β€˜K’ denoting the course duration and the number of days ninja can focus on the course.

The second line of each test case contains β€˜N’ space-separated integers representing the array β€˜EXP’.

The third line of each test case contains β€˜N’ space-separated integers representing the array β€˜BUSY’.
Output Format :
For every test case, print a single line containing a single integer denoting the maximum experience that the ninja can gain from this course.

The output of each test case will be printed in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 100
1 <= N <= 1000
0 <= K <= N
0 <= EXP[i] <= 10 ^ 6
BUSY[i] = {0, 1}

Time limit: 1 second.
Approaches

01Approach

Use β€˜res’ to store the maximum experience that ninja can gain. Initially, ninja focuses on attending the first β€˜K’ days starting from day β€˜0’ (to β€˜K - 1’). Calculate the total experience gained and store it in β€˜res’. Now, focus on the next β€˜K’ days starting from day β€˜1’ (to β€˜K’), and if the total experience gained is more than β€˜res’, then update β€˜res’. Similarly, find the experience gained for each such combination until the last β€˜K’ days, each time updating the value of β€˜res’. 

 

Find the experience gained without requiring any focus(non-busy days) and store it in β€˜directExp’.To find the experience gained after focusing on any β€˜K’ days, iterate through the β€˜K’ days and only add experiences of those days when ninja was busy to β€˜directExp’ (as β€˜directExp’ already stores the experience gained on non-busy days).

 

  • Initialize β€˜res’ to β€˜INT_MIN’. Use β€˜res’ to store the answer. β€˜INT_MIN’  is the lowest possible integer value.
  • Initialize β€˜directExp = 0’. Use to store the experience gained on non-busy days.
  • Run a loop where β€˜i’ ranges from β€˜0’ to β€˜n - 1’:
    • If β€˜BUSY[i]’ is equal to 0, then:
      • β€˜directExp += EXP[i]’
  • Run a loop where β€˜i’ ranges from β€˜0’ to β€˜N - K’:
    • β€˜curExp = directExp’. Use to find the experience gained from busy days if ninja focusses from the day β€˜i’ to β€˜i + K - 1’.
    • Run a loop where β€˜j’ ranges from β€˜i’ to β€˜i + K - 1’:
      • If β€˜BUSY[j]’ is equal to 1, then:
        • β€˜curExp += EXP[j]’
    • If β€˜curExp’ is greater than β€˜res’, then:
      • β€˜res = curExp’.
  • Return β€˜res’ as the answer.