B. 250 Thousand Tons of TNT

time limit per test

2 seconds

memory limit per test

256 megabytes

Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared n boxes and arranged them in a row waiting for trucks. The i-th box from the left weighs ai tons.

All trucks that Alex is going to use hold the same number of boxes, denoted by k. Loading happens the following way:

  • The first k boxes goes to the first truck,
  • The second k boxes goes to the second truck,
  • The last k boxes goes to the nk-th truck.

Upon loading is completed, each truck must have exactly k boxes. In other words, if at some point it is not possible to load exactly k boxes into the truck, then the loading option with that k is not possible.

Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is 0.

Alex has quite a lot of connections, so for every 1≤k≤n, he can find a company such that each of its trucks can hold exactly k boxes. Print the maximum absolute difference between the total weights of any two trucks.

Input

The first line contains one integer t (1≤t≤104) --- the number of test cases.

The first line of each test case contains one integer n (1≤n≤150000) --- the number of boxes.

The second line contains n integers a1,a2,...,an (1≤ai≤109) --- the weights of the boxes.

It is guaranteed that the sum of n for all test cases does not exceed 150000.

Output

For each test case, print a single integer --- the answer to the problem.

Example

Input

Copy

复制代码

5

2

1 2

6

10 2 3 6 1 3

4

1000000000 1000000000 1000000000 1000000000

15

60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294

8

19957 69913 37531 96991 57838 21008 14207 19198

Output

Copy

复制代码
1
9
0
189114
112141

Note

In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box.

In the second case, we should pick six trucks, so the maximum will be 10, the minimum will be 1, and the answer is 10−1=9.

In the third case, for any possible k, the trucks will have the same total weight of boxes, so the answer is 0.

解题说明:此题是一道模拟题,其实就是从数列中找出最大值和最小值然后求差,注意这里最大值和最小值不一定是一个数字,最多可以由连续K个数字组成。循环遍历找出最大值和最小值的最大差值即可。

cpp 复制代码
#include<stdio.h>
int a[150010];
int main()
{
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; i++)
	{
		int n;
		scanf("%d", &n);
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &a[j]);
		}
		long long high = 0;
		for (int j = 1; j < n; j++)
		{
			long long sub = 0;
			if (n % j == 0)
			{
				long long max = 0;
				long long min = 1e18;
				for (int k = 0; k < n / j; k++)
				{
					long long temp = 0;
					for (int z = 0; z < j; z++)
					{
						temp += a[k * j + z];
					}
					if (temp > max)
					{
						max = temp;
					}
					if (temp < min)
					{
						min = temp;
					}
				}
				sub = max - min;
				if (sub > high)
				{
					high = sub;
				}
			}
		}
		printf("%lld\n", high);
	}
	return 0;
}
相关推荐
故事和你9114 小时前
sdut-程序设计基础Ⅰ-实验五一维数组(8-13)
开发语言·数据结构·c++·算法·蓝桥杯·图论·类和对象
像污秽一样14 小时前
算法与设计与分析-习题4.2
算法·排序算法·深度优先·dfs·bfs
Storynone15 小时前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
明明如月学长15 小时前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流
算法
黎阳之光15 小时前
【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
算法·安全·智慧城市·数字孪生
刀法如飞16 小时前
Agentic AI时代,程序员必备的算法思想指南
人工智能·算法·agent
刀法如飞17 小时前
Agentic AI时代程序员必备算法思想详解(附实战案例)
算法·ai编程·编程开发·agentic
飞Link17 小时前
告别盲目找Bug:深度解析 TSTD 异常检测中的预测模型(Python 实战版)
开发语言·python·算法·bug
记忆多18 小时前
c++名字空间 函数模版 左右值
开发语言·c++·算法
三伏52218 小时前
控制理论前置知识——相平面数学基础2(示例部分)
算法·平面·控制