Codeforces Round #621 (Div. 1 + Div. 2)_A. Cow and Haybales(C++ 시뮬레이션)
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 i and j (1≤i,j≤n) such that |i−j|=1 and ai>0 and apply ai=ai−1, aj=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 1 (i.e. to maximize a1), and she only has d days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 1 if she acts optimally!
Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. Next 2t lines contain a description of test cases — two lines per test case.
The first line of each test case contains integers n and d (1≤n,d≤100) — the number of haybale piles and the number of days, respectively.
The second line of each test case contains n integers a1,a2,…,an (0≤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 1 after d days if Bessie acts optimally.
Example
input
3 4 5 1 0 3 2 2 2 100 1 1 8 0
output
3 101 0
Note
In the first test case of the sample, this is one possible way Bessie can end up with 3 haybales in pile 1:
On day one, move a haybale from pile 3 to pile 2 On day two, move a haybale from pile 3 to pile 2 On day three, move a haybale from pile 2 to pile 1 On day four, move a haybale from pile 2 to pile 1 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 2 to pile 1 on the second day.
Process
This problem is easy. Logic can be wrong, but mathematics never.
Code
#include
using namespace std;
int t, n, d;
int main()
{
cin >> t;
for (int i = 0; i < t; i++)
{
int a[1000];
cin >> n >> d;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++)
{
int temp = min(d / i, a[i]);
a[0] += temp;
d -= (temp * i);
if (d == 0)
break;
}
cout << a[0] << endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.