A - Sanitize Hands 解説 by en_translator
The problem can be solved by simulating the remaining amount of disinfectant.
Starting from \(M\), subtract \(H_1,H_2,\ldots,H_N\) from it in order.
If it never goes below \(0\), all aliens can disinfect their hands, so the answer is \(N\). Otherwise, if it becomes less than \(0\) for the first time when subtracting \(H_i\), then the answer is \(i-1\).
Therefore, the problem has been solved.
Appropriate decide whether to have an equal sign to the inequality in a conditional branch, in order to handle the case when the disinfectant is used up just when an alien could disinfect all their hands.
Sample code in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m,h,ans=0;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>h;
m-=h;
if(m>=0)ans++;
}
cout<<ans<<endl;
return 0;
}
Sample code in Python:
n,m=map(int, input().split())
h=list(map(int, input().split()))
ans=0
for i in range(n):
m-=h[i]
if m>=0:
ans+=1
print(ans)
投稿日時:
最終更新: