Official

B - 買い物リスト / Shopping List Editorial by sounansya


前から順番に商品を見ていき、総和が \(X\) 以上になればそのインデックスを出力すれば良いです。

実装例(Python3)

n, x = map(int, input().split())
a = list(map(int, input().split()))
now = 0
for i in range(n):
    now += a[i]
    if now >= x:
        print(i + 1)
        break
else:
    print(-1)

posted:
last update: