Description
There are n
points on a horizontal line, labelled with 1 through n
from left to right.
The distance between the i
-th point and the (i+1)-th point is ai
.
For each integer k
ranged from 1 to n, you are asked to select exactly k
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 T
indicating the number of test cases which is up to 1000
.
For each test case, the first line contains an integer n
indicating the number of points, where 2≤n≤105
.
The second line contains (n−1)
positive integers a1,a2,⋯,an−1, where 1≤ai≤104
.
We guarantee that the sum of n
in all test cases is up to 106
.
Output
For each test case, output a line containing n
integers, the i-th of which is the maximum sum of distances in case k=i
. You should output exactly one whitespace between every two adjacent numbers and avoid any trailing whitespace in this line.
Sample Input
Input
1 5 2 3 1 4
Output
0 10 20 34 48
Hint
The figure below describes the sample test case.
The only best selection for k=2
should choose the leftmost and the rightmost points, while a possible best selection for k=3 could contain any extra point in the middle.
#include<stdio.h>
#include <bits/stdc++.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 400000
#define ll long long
#include<vector>
ll a[105000],c[105000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll n;
scanf("%lld",&n);
for(int i=1;i<=n+100;i++)
{
a[i]=c[i]=0;
}
a[1]=1;
for(ll i=2;i<=n;i++)
{
ll x;
scanf("%lld",&x);
a[i]=a[i-1]+x;
}
for(ll i=1;i<=n/2;i++)
{
c[i]+=c[i-1]+a[n-i+1]-a[i];
}
ll ans=0;
printf("0 ");
for(ll i=2;i<=n;i++)
{
ans=ans+c[i/2];
if(i==n)printf("%lld\n",ans);
else printf("%lld ",ans);
}
}
}