꺽일때마다(극점) 사거나 팔아주면 된다. import sys input = sys.stdin.readline N, W = map(int, input().split( )) costs = [int(input()) for __ in range(N)] coin = 0 for i in range(1, N): if not coin: if costs[i - 1] costs[i]: W += coin * costs[i - 1] coin = 0 if coin: W += coin * costs[N - 1] print(W)