The first line contains an integers n(1≤n≤300,000).
The second line contains n integers describing the given array A, the ith integer is Ai(1≤Ai≤1,000,000). Between each two adjacent integers there is a white space separated.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lth th<<1
#define rth th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 300050
int b[maxn],a[maxn];
ll sum[maxn];
int L[maxn],R[maxn];
int q[511111][2];
int main()
{
int n,m,i,j,T,l,r,k;
int front,rear;
ll ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
ans=0;
for(k=1;k<=n;k++){
int tot=0;
sum[0]=0;
for(i=1;i*k<=n;i++){
tot++;
b[tot]=a[i*k];
sum[tot]=sum[tot-1]+b[tot];
}
front=1,rear=0;
for(i=1;i<=tot;i++){
while(front<=rear && q[rear][0]>=b[i] ){
rear--;
}
if(rear==0){
L[i]=1;
}
else{
L[i]=q[rear][1]+1;
}
rear++;
q[rear][0]=b[i];q[rear][1]=i;
}
front=1,rear=0;
for(i=tot;i>=1;i--){
while(front<=rear && q[rear][0]>=b[i] ){
rear--;
}
if(rear==0){
R[i]=tot;
}
else{
R[i]=q[rear][1]-1;
}
rear++;
q[rear][0]=b[i];q[rear][1]=i;
ans=max(ans,(sum[R[i] ]-sum[L[i]-1 ])*b[i]*(int)sqrt((double)k) );
}
}
printf("%lld\n",ans);
}
return 0;
}