#38454: 解答


115205@tchcvs.tw (114級115205資2班.張楷昊)

學校 : 國立臺中高級家事商業職業學校
編號 : 233256
來源 : [111.252.111.133]
最後登入時間 :
2024-04-24 15:22:06
h081. 1. 程式交易 -- 2022年1月APCS | From: [36.232.162.24] | 發表日期 : 2023-11-24 20:18

a,b=input().split(' ')
a,b=int(a),int(b)
c=input().split(' ')
c=list(map(int,c))
gs=0
gc=1
gt=c.pop(0)
for i in c:
#    print(i,gs,'g',gt)
    if i>=gt+b and gc==1 :#賣出
#        print('b')
        gs+=i-gt
        gt=i
        gc=0
    elif i<gt-b and gc!=1:
#        print('a')
#        gs-=i
        gt=i
        gc=1
print(gs)

 
ZeroJudge Forum