Ninja wants to go on a vacation in his summer holidays. But he got a lot of homework from his school which requires a certain amount of time to complete. As the ninja was the topper of the class he was allowed to skip tasks, but cannot skip two consecutive tasks from his homework. You are given an array, ‘ARR’ of ‘N’ integers. Find the minimum time ninja needs to spend to complete the homework.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains an integer ‘N’, representing the number of tasks.
The second line of each test case contains ‘N’ space-separated integers, representing the time needed to complete each task.
Output Format :
For each test case, print the minimum time needed to complete the homework.
Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5*10^3
Time Limit : 1 sec
2
4
10 5 7 10
2
10 30
12
10
For the first test case :
We can skip the first and the last tasks to complete the homework in 12 units of time.
For the second test :
We can skip the last task to complete the homework in 10 units of time.
2
8
10 5 2 4 8 6 7 10
4
1 2 3 4
22
4
Try to check time if we include the ‘ith’ task or exclude it.
For any ‘ith’ task we have two choices with us whether to include the ‘ith’ task or exclude the ‘ith’ task.
We can maintain an array to keep track of time.
Here is the algorithm :
O(N), where ‘N’ is the number of tasks.
We traverse all the tasks once to find the time’. Therefore, the overall time complexity will be O(N).
O(N), where ‘N’ is the number of tasks.
We use two extra arrays to maintain the time of including and excluding the tasks. Therefore, the overall space complexity will be O(N).