Train of Threes
Time limit5sMemory limit256 MB
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 numbers. Each number is , or , or of the form for some integer .
You can combine two matching adjacent numbers into a larger number. A next to a combines into a single . Two adjacent s combine into a single , two adjacent s combine into a single , and in general two identical adjacent numbers that are at least combine into a single number equal to their sum. The numbers and are special: a matches only a , and a matches only a .
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 , the largest number you can form is .
Input
The first line contains an integer , the number of test cases (). Then follow lines. The first line of each test case contains an integer , the size of the array (). The next line contains integers, where each integer is , , or of the form for some integer .
Output
For each of the test cases, print the largest number that can be formed on its own line.