This page is still under construction.

Parts of this page are still being built. What you see may change.

Train of Threes

Time limit5sMemory limit256 MB

Summary
You repeatedly merge adjacent matching pairs in an array of 1s, 2s and numbers of the form 3 times a power of two to form the largest tile possible.
Level

Hard8 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

Threes! is a puzzle game available on iOS, Android, and other platforms. The rules are simple, but the structure behind them is deep: you repeatedly match adjacent numbers to build larger numbers. Here you play a simplified version of the game.

The game starts with an array of NN numbers. Each number is 11, or 22, or of the form 3×2i3 \times 2^i for some integer i≥0i \ge 0.

You can combine two matching adjacent numbers into a larger number. A 11 next to a 22 combines into a single 33. Two adjacent 33s combine into a single 66, two adjacent 66s combine into a single 1212, and in general two identical adjacent numbers that are at least 33 combine into a single number equal to their sum. The numbers 11 and 22 are special: a 11 matches only a 22, and a 22 matches only a 11.

The goal of the game is to form the largest number possible. The game ends when no two adjacent numbers can be combined. For example, starting from the array {12,24,6,1,2,3,12,3,3,6,1,2,2,1,24}\{12, 24, 6, 1, 2, 3, 12, 3, 3, 6, 1, 2, 2, 1, 24\}, the largest number you can form is 4848.

Input

The first line contains an integer TT, the number of test cases (1≤T≤10001 \le T \le 1000). Then follow 2T2T lines. The first line of each test case contains an integer NN, the size of the array (1≤N≤100001 \le N \le 10000). The next line contains NN integers, where each integer is 11, 22, or of the form 3×2i3 \times 2^i for some integer 0≤i≤110 \le i \le 11.

Output

For each of the TT test cases, print the largest number that can be formed on its own line.

Examples2

  1. Example 1

    Input
    3
    5
    1 2 2 2 2
    6
    24 12 6 3 2 1
    4
    3 3 3 3
    
    Expected output
    3
    48
    12
    
  2. Example 2

    Input
    1
    15
    12 24 6 1 2 3 12 3 3 6 1 2 2 1 24
    
    Expected output
    48