Under construction

Still being built. Things may change

Windblume Festival

Time limit3sMemory limit1024 MB

Summary
Players in a circle repeatedly remove a neighbor and subtract its value from the player before it, and the task is to find the maximum value the last player can hold.
Level

Medium6 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

The Windblume Festival in Mondstadt is coming! People are preparing windblumes for Barbatos and for those they love and adore. The Windblume Festival is also an opportunity to improve the relationships people have. Source: Genshin Impact Official. During the festival, a famous game is played every year, invented by Jean, the Acting Grand Master of the Knights of Favonius. In the game, nn players numbered from 11 to nn stand in a circle, each holding an integer. Each turn, one player is removed. The game ends when only one player is left. For each turn, let kk be the number of players remaining and a_ia\_i be the integer player ii holds. Two adjacent players, xx and (x mod k+1)(x \bmod k + 1), are selected, and player (x mod k+1)(x \bmod k + 1) is removed from the game. Player xx's integer then changes from a_xa\_x to (a_x−a_x mod k+1)(a\_x - a\_{x \bmod k + 1}). Player yy in this turn becomes player (y−1)(y - 1) in the next turn for all x<y≤kx < y \le k, though the integer they hold does not change. Jean wants to know the maximum possible integer held by the last remaining player by selecting the players in each round optimally.

Input

There are multiple test cases. The first line of the input contains one integer TT, the number of test cases. For each test case: The first line contains one integer nn (1≤n≤1061 \le n \le 10^6), the initial number of players. The next line contains nn integers a_ia\_i (−109≤a_i≤109-10^9 \le a\_i \le 10^9), where a_ia\_i is the integer held by player ii at the beginning. It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

Output

For each test case, output one line containing one integer, the maximum possible integer.

Hint

For the first sample test case, follow the strategy shown below, where the underlined integers are the integers held by the players selected in each turn. {1‾,−3,2,−4‾}\{\underline{1}, -3, 2, \underline{-4}\} (select x=4x = 4) →\to {−3,2,−5‾}\{-3, \underline{2, -5}\} (select x=2x = 2) →\to {−3,7‾}\{\underline{-3, 7}\} (select x=2x = 2) →\to {10}\{10\}.

Examples1

  1. Example 1

    Input
    5
    4
    1 -3 2 -4
    11
    91 66 73 71 32 83 72 79 84 33 93
    12
    91 66 73 71 32 83 72 79 84 33 33 93
    13
    91 66 73 71 32 83 72 79 84 33 33 33 93
    1
    0
    
    Expected output
    10
    713
    746
    779
    0