sub max{$_[0]>$_[1]?$_[0]:$_[1]} ($n,$d)=glob<>; $t=0;$k=-$d; for(<>){ / /; ($t,$k)=(max($t+$`,$k+$'-$d),max($t+$`-$d,$k+$')); } print max($t,$k);