Distance

There are nn points on a horizontal line, labelled with 11 through nn from left to right.

The distance between the ii-th point and the (i+1)(i+1)-th point is aiai.

For each integer kk ranged from 11 to nn, you are asked to select exactly kk different given points on the line to maximize the sum of distances between all pairs of selected points.

Input

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 10001000.

For each test case, the first line contains an integer nn indicating the number of points, where 2≤n≤1052≤n≤105.

The second line contains (n−1)(n−1) positive integers a1,a2,⋯,an−1a1,a2,⋯,an−1, where 1≤ai≤1041≤ai≤104.

We guarantee that the sum of nn in all test cases is up to 106106.

Output

For each test case, output a line containing nn integers, the ii-th of which is the maximum sum of distances in case k=ik=i. You should output exactly one whitespace between every two adjacent numbers and avoid any trailing whitespace in this line.

Example

Input

1
5
2 3 1 4

Output

0 10 20 34 48

 题意:

线段上有n个点,相邻两点之间的距离为第三行输入,求分别取线段上k(1-n)个点,线段上点两两之间距离的最大值。

解题思路:

脱机用手画图可以找到规律,选点从两边开始选值是最大的(很好证明,自己可是试一下),用L,R分别从两个方向进行加点,用数组a存各点到点的距离(通过到原点的距离可以求出任意两点之间的距离),模拟操作两组点之后可以发现每一次的操作和上一次都是有关系的,左右边界逐渐缩短的迭代操作。

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

typedef long long ll;
const int maxn = 1e5+2;
long long a[maxn];
int n;
int x;
ll result,mid;
int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(a,0,sizeof(a));
        cin>>n;
        for(int i = 2;i<=n;i++){
            cin>>x;
            a[i] += a[i-1]+x;
        }
        result = 0;
        mid = 0;//记中间数
        int l = 0;
        int r = n+1;
//        for(int i = 1;i<=n;i++)
//            cout<<a[i]<<" ";
        for(int i = 1;i<= n;i++){
            if(i&1){ // 奇数个顶点
                l++;
                result += mid;
            }
            else{
                r--;
                mid += a[r] - a[l];
                result += mid;
            }
            if(i == 1)
                cout<<result;
            else
                cout<<" "<<result;
        }
        cout<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值