Buggy python submission (verdict: Time Limit Exceeded):
# abc 154
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N, K = map(int, readline().split())
P = list(map(int, readline().split()))

def cal_expected(p):
    expected = 0
    for i in range(1, p+1):
        expected += i / p
    return expected

# 期待値一覧の作成
expected_dict = {k:cal_expected(k) for k in set(P)}

# 対象特定
dices = []
dices_sum = 0
for start in range(0, N-K+1):
    tmp_dices = P[start:start+K]
    tmp_sum = sum(tmp_dices)
    if tmp_sum > dices_sum:
        dices = tmp_dices
        dices_sum = tmp_sum

# 期待値計算
# answer = sum(map(cal_expected, dices))
answer = 0
for dice in dices:
    answer += expected_dict[dice]
print(answer)


Fixed submission:
# abc 154
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N, K = map(int, readline().split())
P = list(map(int, readline().split()))

def cal_expected(p):
    expected = 0
    for i in range(1, p+1):
        expected += i / p
    return expected

# 期待値一覧の作成
expected_dict = {k:cal_expected(k) for k in set(P)}

import copy
from collections import deque
# 対象特定
ans_start = 0
dices_sum = 0
tmp_dices = None
for start in range(0, N-K+1):
    if tmp_dices == None:
        tmp_dices = deque(P[start:start+K])
        tmp_sum = sum(tmp_dices)
    else:
        removed = tmp_dices.popleft()
        tmp_dices.append(P[start+K-1])
        tmp_sum = tmp_sum - removed + P[start+K-1]
    if tmp_sum > dices_sum:
        ans_start = start
        dices_sum = tmp_sum

# 期待値計算
# answer = sum(map(cal_expected, dices))
answer = 0
dices = P[ans_start:ans_start+K]
for dice in dices:
    answer += expected_dict[dice]
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
# abc 154
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N, K = map(int, readline().split())
P = list(map(int, readline().split()))

def cal_expected(p):
    expected = 0
    for i in range(1, p+1):
        expected += i / p
    return expected

# 対象特定
dices = []
dices_sum = 0
for start in range(0, N-K+1):
    tmp_dices = P[start:start+K]
    tmp_sum = sum(tmp_dices)
    if tmp_sum > dices_sum:
        dices = tmp_dices
        dices_sum = tmp_sum

# 期待値計算
answer = sum(map(cal_expected, dices))
print(answer)


Fixed submission:
# abc 154
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N, K = map(int, readline().split())
P = list(map(int, readline().split()))

def cal_expected(p):
    expected = 0
    for i in range(1, p+1):
        expected += i / p
    return expected

# 期待値一覧の作成
expected_dict = {k:cal_expected(k) for k in set(P)}

import copy
from collections import deque
# 対象特定
ans_start = 0
dices_sum = 0
tmp_dices = None
for start in range(0, N-K+1):
    if tmp_dices == None:
        tmp_dices = deque(P[start:start+K])
        tmp_sum = sum(tmp_dices)
    else:
        removed = tmp_dices.popleft()
        tmp_dices.append(P[start+K-1])
        tmp_sum = tmp_sum - removed + P[start+K-1]
    if tmp_sum > dices_sum:
        ans_start = start
        dices_sum = tmp_sum

# 期待値計算
# answer = sum(map(cal_expected, dices))
answer = 0
dices = P[ans_start:ans_start+K]
for dice in dices:
    answer += expected_dict[dice]
print(answer)