Buggy python submission (verdict: Time Limit Exceeded):

N,X =  map(int, input().split())

a = list(map(int, input().split()))


ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X
    dp = []
    dpnum = []

    for i in range(1,N+1,k):
        sum = 0
        num = 0
        for j in range(i,min(i+k,N+1)):
            sum += a[N-j]
            num += 1
        dp.append(sum)
        dpnum.append(num)


    count = 0
    for i in range(len(dp)):
        if i == 0:
            cur += dp[i] * 5
        else:
            cur += dp[i] * (2*(i+1) + 1)

    """
    for i in range(1,N+1,k):

        num = min(i+k-1,N) - i + 1

        if count == 0:
            cur += dp[count] * 5 * num
        else:
            cur += dp[count] * (2*(count+1) + 1) * num

        
        count += 1
    """
        
    ### print(dp, dpnum, cur)
    ans = min(ans, cur)

print(ans)



Fixed submission:

N,X =  map(int, input().split())

a = list(map(int, input().split()))

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,X =  map(int, input().split())

a = list(map(int, input().split()))

dp = [[-1 for i in range(N+10)] for j in range(N+10)]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X

    for i in range(1,N+1):
        pos = (i + k - 1) // k

        if dp[pos][N-i] != -1:
            cur += dp[pos][N-i]
            continue

        if pos == 1:
            dp[pos][N-i] =  5*a[N-i]
        else:
            dp[pos][N-i] = (2*pos+1) * a[N-i]
        
        cur += dp[pos][N-i]
    
    ans = min(ans, cur)

print(ans)

Fixed submission:

N,X =  map(int, input().split())

a = list(map(int, input().split()))

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,X =  map(int, input().split())

a = list(map(int, input().split()))


ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X

    for i in range(1,N+1):
        pos = (i + k - 1) // k
        if pos == 1:
            cur += 5*a[N-i]
        else:
            cur += (2*pos+1) * a[N-i]
    
    ans = min(ans, cur)

print(ans)


Fixed submission:

N,X =  map(int, input().split())

a = list(map(int, input().split()))

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)

Buggy python submission (verdict: Runtime Error):
N,X =  map(int, input().split())

a = list(map(int, input().split()))
a.sort(reverse=True)

dp = [[-1 for i in range(N+10)] for j in range(N+10)]


def deposit(n, trash):
    if n + 1 == N:
        return X + a[n] * ((trash+1) ** 2)

    if dp[n][trash] != -1:
        return dp[n][trash]

    #もってるゴミをかえすか、そのまま次のゴミをひろうか？
    dp[n][trash] = min(X+a[n]*((trash+1)**2)+X+a[n+1]+deposit(n+1,1),(a[n]-a[n+1])*((trash+1)**2)+X+deposit(n+1,trash+1))
    return dp[n][trash]


print(deposit(0,1)+X+a[0])

Fixed submission:

N,X =  map(int, input().split())

a = list(map(int, input().split()))

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)

Buggy python submission (verdict: Runtime Error):

N,X =  map(int, input().split())

a = list(map(int, input().split()))
a.sort(reverse=True)

def deposit(n, trash):
    if n + 1 == N:
        return X + a[n] * ((trash+1) ** 2)
    
    return min(X+a[n]*((trash+1)**2)+X+a[n+1]+deposit(n+1,1),(a[n]-a[n+1])*((trash+1)**2)+X+deposit(n+1,trash+1))

print(deposit(0,1)+X+a[0])


Fixed submission:

N,X =  map(int, input().split())

a = list(map(int, input().split()))

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)