#P9852. [ICPC2021 Nanjing R] Windblume Festival

[ICPC2021 Nanjing R] Windblume Festival

题目描述

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.

During the festival, a famous game will be 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 with them. Each turn, one player will be removed. The game will end when there is only one player left.

For each turn, let kk be the number of players remaining and aia_i be the integer player ii holds. Two adjacent players, xx and (xmodk+1)(x \bmod k + 1) are selected and player (xmodk+1)(x \bmod k + 1) is removed from the game. Player xx's integer will then change from axa_x to (axaxmodk+1)(a_x - a_{x \bmod k + 1}). Player yy in this turn will become player (y1)(y - 1) in the next turn for all x<ykx < y \le k, though the integer they hold will not change.

Jean wants to know the maximum possible integer held by the last remaining player in the game by selecting the players in each round optimally.

输入格式

There are multiple test cases. The first line of the input contains one integer TT indicating the number of test cases. For each test case:

The first line contains one integer nn (1n1061 \le n \le 10^6) indicating the initial number of players.

The next line contains nn integers aia_i (109ai109-10^9 \le a_i \le 10^9) where aia_i is the integer held by player ii at the beginning.

It is guaranteed that the sum of nn of all test cases will not exceed 10610^6.

输出格式

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

题目大意

给一个长度为 nn 环形整数序列 aa, 每次操作可以任意选择一个下标 xx,令 ax=axa(xmodn)+1 a_x = a_x - a_{(x\bmod n)+1},之后移除 a(xmodn)+1a_{(x\bmod n)+1}

最后会剩下一个元素,要求最大化这个元素。

数据范围 1n106,109ai1091\le n\le10^6,-10^9\le a_i\le 10^9

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

10
713
746
779
0

提示

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\}.