import numpy as np n, m = map(int, input().split()) c = [0] + list(map(int, input().split())) # mが1の時だけ除外(エラー回避) if m == 1: print(sum(c)) exit() # P_list[pos]: posマス目がstartとみなしたときのゴールまでの期待値(魔法なし) P_list = [0]*(n-1) # Q_list[pos]: posマス目がstartとみなしたときのゴールまでの期待値(魔法あり) Q_list = [0]*(n-1) # P_listのゴール手前m-1マスを計算(線形代数) # 一次線形方程式を解く key = (m+1) // 2 mat = np.matrix([[0]*(key)]*(key)) vec = np.array([[0]*(key)]).T for i in range(key): tmp = [0]*(key) count = m - 1 pos = i while count > 0 and pos >= 0: tmp[pos] = tmp[pos] - 1 pos = pos - 1 count = count - 1 while count > 0: tmp[pos] = tmp[pos] - 1 pos = pos + 1 count = count - 1 tmp[pos] = tmp[pos] + m mat[i] = tmp vec[i] = sum([c[n-3-j]*tmp[j] for j in range(key)]) inv_mat = np.linalg.inv(mat) vec = inv_mat.dot(vec) # P_listの初期化 for i in range(key): P_list[n-2-i] = vec[i][0] P_list[n-m-1+i] = vec[i][0] # 動的計画法 for pos in range(n-m-2, -1, -1): # P_list の更新 P_list[pos] = sum([c[i]+P_list[i] for i in range(pos+1, pos+m+1)]) / m # Q_list の更新 Q_list[pos] = min([c[i]+P_list[i] for i in range(pos+1, pos+m+1)]\ +[sum([c[i]+Q_list[i] for i in range(pos+1, pos+m+1)])/m]) print(Q_list[0])