DCEPC501 - Save Thy Toys


Leonard is very fond of buying rare and expensive science fiction toys. He keeps his collection in a sequential order of the date on which the toy was bought in a special closet so that his roomie Sheldon never gets hold of his toys. But because of his bad luck Leonard once loses a bet to Sheldon and Sheldon demands a share Leonard’s toys. Since Leonard doesn’t want to loose much money, he decides upon a strategy to reduce his loss to minimum.

Leonard, beginning from the first toy in his closet will pick some toys, say "x" toys in sequence. Sheldon will then pick the next "x" toys (Note that Sheldon picks equal no. of toys as picked by Leonard in his move unless the remaining toys are less than "x". In that case he picks all of the remaining). This will keep going on till no more toys are left in the closet for Leonard to pick. You are given the sequence of toys with their price. Help Leonard in maximizing the total price of all toys he picks.

Leonard in his each turn can either pick only 1 or 2 or 3 toys ("x" described above can take value either 1, 2 or 3).

Input

First line specifies T, the number of test cases.

Each test case contains N in the first line. Second line contains N integers as described above.

Output

Output 1 line for each test case giving the maximum possible value of the total sum of all toys Leonard picks.

Constraints

1<=T<=10

1<=N<=100000

1<=Price of toys<=1000000

Example

Input:
2
4
5 4 3 2
6
10 8 7 11 15 20

Output:
12
53

Explanation:

In 1st case, Leonard picks 3 toys in his first move with value 5,4,3 and Sheldon has no choice but to pick the last.
In 2nd case, Leonard picks 10, 8. Then Sheldon picks 7,11. And then Leonard picks the rest.


hide comments
kira28: 2016-12-23 14:58:06

java.util.Scanner - TLE :(
java.io.DataInputStream - AC!!! XD XD
#1 on ranks yayy

subh1685: 2016-12-08 06:04:38

some tricky tst case PLEASE,dont know why m gttng WA

rraj001: 2016-10-14 08:47:37

top down:easy one :)

yash_18: 2016-10-12 22:02:20

So after an hour of misery change int to long long int,& use printf and scanf to get AC!wow... O_o

conquistador: 2016-10-02 20:55:51

memoization required

conquistador: 2016-10-02 20:55:18

solved it with the help of siddharth

jiyan7: 2016-10-02 11:20:51

ac in one go>>easy>>memoization>>topdown>>ios ::sync_with_stdio(flase);

saini428: 2016-06-20 06:55:13

simple problem, my 100th

singh1495: 2016-06-19 21:48:38

donot confuse on previous comment simply apply topdown + memoization

Siddharth: 2016-06-15 16:34:29

Please extend time-limit for python (both versions). Same solution works with C++


Added by:dce coders
Date:2012-04-18
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ASM32-GCC MAWK BC C-CLANG C NCSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JAVA JS-MONKEY JULIA KTLN NIM NODEJS OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 PY_NBC R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET
Resource:Own Problem