def main(): import sys input = sys.stdin.read().split() idx = 0 n, m = int(input[idx]), int(input[idx+1]) idx +=2 if n > 2: c = list(map(int, input[idx:idx+n-2])) else: c = [] cost = [0.0]*(n+1) for i in range(2, n): cost[i] = c[i-2] # Precompute next_pos[i][j] next_pos = [[0]*(m+1) for _ in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): current = i for _ in range(j): if current < n: current +=1 else: current -=1 next_pos[i][j] = current # Initialize E0 and E1 E0 = [0.0]*(n+1) E1 = [0.0]*(n+1) epsilon = 1e-12 max_iter = 100000 # Prevent infinite loop for _ in range(max_iter): new_E0 = [0.0]*(n+1) new_E1 = [0.0]*(n+1) max_error = 0.0 for i in range(1, n+1): if i == n: new_E0[i] = 0.0 new_E1[i] = 0.0 continue # Compute E1[i] total1 = 0.0 for j in range(1, m+1): next_i = next_pos[i][j] total1 += E1[next_i] new_E1_i = cost[i] + total1 / m new_E1[i] = new_E1_i # Compute E0[i] min_e1 = float('inf') for j in range(1, m+1): next_i = next_pos[i][j] if E1[next_i] < min_e1: min_e1 = E1[next_i] option1 = cost[i] + min_e1 total2 = 0.0 for j in range(1, m+1): next_i = next_pos[i][j] total2 += E0[next_i] option2 = cost[i] + (total2 / m) new_E0_i = min(option1, option2) new_E0[i] = new_E0_i # Update max_error max_error = max(max_error, abs(new_E0_i - E0[i]), abs(new_E1_i - E1[i])) # Check convergence if max_error < epsilon: break E0, new_E0 = new_E0, E0 E1, new_E1 = new_E1, E1 print("{0:.10f}".format(E0[1])) if __name__ == '__main__': main()