#32742: 解題


hung19741212@gmail.com (黃老)

學校 : 不指定學校
編號 : 81939
來源 : [180.218.45.146]
最後登入時間 :
2023-06-13 06:04:51
h081. 1. 程式交易 -- 2022年1月APCS | From: [180.217.201.157] | 發表日期 : 2022-11-02 21:38

n,d=map(int,input().split())
a=[int(x) for x in input().split()]
share=True 
buy=a[0]
profit=0
for i in range(0,n):
  if share:
    if a[i]>=buy+d:
      profit += a[i]-buy
      sale=a[i]
      share=False 
  else:
    if a[i]<=sale-d:
      buy=a[i]
      share=True 
print(profit)

 
ZeroJudge Forum