Coin Row

Interview

Time limit1sMemory limit128 MB

Summary
Given rows of coin values, pick non-adjacent coins to maximize the total collected in each row.
Level

Easy3 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

A row of nn coins has positive integer values c1,c2,…,cnc_1, c_2, \dots, c_n (not necessarily distinct). Select coins so that the total value collected is as large as possible, subject to the constraint that no two adjacent coins may be selected.

Input

The first line contains a positive integer nn, the number of coin rows that follow. Each of the next nn lines describes one coin row as a list of positive integers separated by one or more spaces. A row contains at most 20 coins.

Output

For each coin row, print on its own line the maximum total value you can collect subject to the constraint that no two adjacent coins are selected. Print each answer as a plain positive integer with no extra formatting.

Examples1

  1. Example 1

    Input
    2
    5 1 2 10 6 2
    22 55 66 55 15 10
    
    Expected output
    17
    120