program main implicit none integer*8::N,A,B,W,i,str,str_max,tmp,dp_min integer*8,allocatable::D(:) integer*8,allocatable::dp(:) read *, N,A,B,W allocate(D(N)) allocate(dp(0:N)) read *, D if(B.eq.0) then print '(i0)', (W-N*A) return end if dp(0) = W dp(1) = W - A + B*1 dp(0) = W + D(1) str_max = 1 tmp = MIN(dp(0),dp(1)) do i=2,N if(tmp.eq.dp(0)) then str_max = 0 end if tmp = tmp + D(i) dp_min = tmp str_max = MIN(str_max, (D(i)+A)/B) do str=str_max+1,1,-1 dp(str) = dp(str-1) - A + B*str dp_min = MIN(dp_min, dp(str)) end do dp(0) = tmp str_max = str_max + 1 tmp = dp_min end do print '(i0)', MINVAL(dp(0:str_max)) end program main