The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains a_iai haybales.
However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices ii and jj (1 \le i, j \le n1≤i,j≤n) such that |i-j|=1∣i−j∣=1 and a_i>0ai>0 and apply a_i = a_i - 1ai=ai−1, a_j = a_j + 1aj=aj+1. She may also decide to not do anything on some days because she is lazy.
Bessie wants to maximize the number of haybales in pile 11 (i.e. to maximize a_1a1), and she only has dd days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 11 if she acts optimally!
Input
The input consists of multiple test cases. The first line contains an integer tt (1 \le t \le 1001≤t≤100) — the number of test cases. Next 2t2t lines contain a description of test cases — two lines per test case.
The first line of each test case contains integers nn and dd (1 \le n,d \le 1001≤n,d≤100) — the number of haybale piles and the number of days, respectively.
The second line of each test case contains nn integers a_1, a_2, \ldots, a_na1,a2,…,an (0 \le a_i \le 1000≤ai≤100) — the number of haybales in each pile.
Output
For each test case, output one integer: the maximum number of haybales that may be in pile 11 after dd days if Bessie acts optimally.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 4 5 1 0 3 2 2 2 100 1 1 8 0 | 3 101 0 |
Note
In the first test case of the sample, this is one possible way Bessie can end up with 33 haybales in pile 11:
- On day one, move a haybale from pile 33 to pile 22
- On day two, move a haybale from pile 33 to pile 22
- On day three, move a haybale from pile 22 to pile 11
- On day four, move a haybale from pile 22 to pile 11
- On day five, do nothing
In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile 22 to pile 11 on the second day.
题意:
有n个干草堆,可以在相邻的草堆之间搬草,每次可以搬一个草,给你d次搬运的机会以及每个草堆中草的数量,问第一个草堆最多可以有多少草。
思路:
直接模拟,先从第二个草堆搬到第一个草堆,每个草花费1次搬运;再从第三个草堆搬到第一个草堆,每个草花费2次搬运...直到搬运次数不足。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
void slove()
{
int t;
cin>>t;
while(t--)
{
int ans=0;
int n,d;
cin>>n>>d;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
if(k*i<=d)
{
ans+=k;
d-=k*i;
}
else
{
ans+=d/i;
d=0;
}
}
cout<<ans<<endl;
}
}
int main()
{
slove();
}