# 2018年 ACM/ICPC亚洲区域赛 青岛赛区现场赛 E题（ZOJ 4062 二分+思维）

Plants vs. Zombies

Time Limit: 2 Seconds      Memory Limit: 65536 KB

BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies. Plants vs. Zombies(?)
(Image from pixiv. ID: 21790160; Artist: socha)

There are  plants in DreamGrid's garden arranged in a line. From west to east, the plants are numbered from 1 to  and the -th plant lies  meters to the east of DreamGrid's house. The -th plant has a defense value of  and a growth speed of . Initially,  for all .

DreamGrid uses a robot to water the plants. The robot is in his house initially. In one step of watering, DreamGrid will choose a direction (east or west) and the robot moves exactly 1 meter along the direction. After moving, if the -th plant is at the robot's position, the robot will water the plant and  will be added to . Because the water in the robot is limited, at most  steps can be done.

The defense value of the garden is defined as . DreamGrid needs your help to maximize the garden's defense value and win the game.

• Each time the robot MUST move before watering a plant;
• It's OK for the robot to move more than  meters to the east away from the house, or move back into the house, or even move to the west of the house.

Input

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

The first line contains two integers  and  (, ), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains  integers  (), where  indicates the growth speed of the -th plant.

It's guaranteed that the sum of  in all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum defense value of the garden DreamGrid can get.

Sample Input

2
4 8
3 2 6 6
3 9
10 10 1


Sample Output

6
4


Hint

In the explanation below, 'E' indicates that the robot moves exactly 1 meter to the east from his current position, and 'W' indicates that the robot moves exactly 1 meter to the west from his current position.

For the first test case, a candidate direction sequence is {E, E, W, E, E, W, E, E}, so that we have  after the watering.

For the second test case, a candidate direction sequence is {E, E, E, E, W, E, W, E, W}, so that we have  after the watering.

#include<bits/stdc++.h>
#define ll long long
#define maxn 200010
using namespace std;
ll n,m,k;
ll c[maxn],a[maxn];
ll ans,tmp,sum;
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}

inline void print(ll x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)
print(x/10);
putchar(x%10+'0');
}
bool jud(ll mid)
{
for(int i=1;i<=n;i++) c[i]=0;
ll cnt=m-1;c=a;
int i=1;
for(;i<=n;i++)
if(i<n){
if(cnt==0) break;
ll tmp=(mid-c[i]+a[i]-1)/a[i];
if(tmp<=0) {cnt--;c[i+1]+=a[i+1];continue;}
if(cnt<tmp*2) break;
cnt-=tmp*2;
c[i]+=a[i]*tmp;
c[i+1]+=a[i+1]*tmp;
if(cnt==0) break;
cnt--;
c[i+1]+=a[i+1];
if(cnt==0) break;
}
else
{
if(cnt==0) break;
if(c[i]>=mid) break;
else
{
ll tmp=(mid-c[i]+a[i]-1)/a[i];
if(cnt<tmp*2) return 0;
else c[i]+=a[i]*tmp;
}
}
for(;i<=n;i++)if(c[i]<mid) return 0;
return 1;
}
int main()
{
ll T,cas=1;
while(T--)
{
ll l=0,r=m;
for(int i=1;i<=n;i++)
if(r<m*a[i]) r=m*a[i];
}
if(m==0) {puts("0");continue;}
ll ans=r;
while(l<r)
{
ll mid=(l+r)/2;
if(jud(mid)) {l=mid+1;ans=mid;}
else r=mid;
}
print(ans);
putchar('\n');
}
return 0;
} 