Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[0.0] * (303) for _ in range(303)] for _ in range(303)]
    for c3 in range(N + 1):
        for c2 in range(N + 1):
            for c1 in range(N + 1):
                sm = c1 + c2 + c3
                if sm == 0:
                    continue
                dp[c1][c2][c3] = 1.0 * N / sm
                if c1:
                    dp[c1][c2][c3] += dp[c1 - 1][c2][c3] * c1 / sm
                if c2:
                    dp[c1][c2][c3] += dp[c1 + 1][c2 - 1][c3] * c2 / sm
                if c3:
                    dp[c1][c2][c3] += dp[c1][c2 + 1][c3 - 1] * c3 / sm
    print(dp[one][two][three])


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]
    for sushi in range(1, N + 1):
        for k in range(sushi + 1):
            for j in range(0, sushi - k + 1):
                i = sushi - k - j
                if i != 0:
                    dp[i][j][k] += dp[i - 1][j][k] * i / sushi
                if j != 0:
                    dp[i][j][k] += dp[i + 1][j - 1][k] * j / sushi
                if k != 0:
                    dp[i][j][k] += dp[i][j + 1][k - 1] * k / sushi
                dp[i][j][k] += N / sushi
    print(dp[one][two][three])


if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    def rec(i, j, k):
        if dp[i][j][k] >= 0:
            return dp[i][j][k]
        if i == 0 and j == 0 and k == 0:
            return 0.0

        res = 0.0
        if i > 0:
            res += rec(i - 1, j, k) * i
        if j > 0:
            res += rec(i + 1, j - 1, k) * j
        if k > 0:
            res += rec(i, j + 1, k - 1) * k
        res += N
        res *= 1.0 / (i + j + k)
        dp[i][j][k] = res

        return dp[i][j][k]

    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[-1] * (N + 1) for _ in range(N + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0.0
    print(rec(one, two, three))


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]
    for sushi in range(1, N + 1):
        for k in range(sushi + 1):
            for j in range(0, sushi - k + 1):
                i = sushi - k - j
                if i != 0:
                    dp[i][j][k] += dp[i - 1][j][k] * i / sushi
                if j != 0:
                    dp[i][j][k] += dp[i + 1][j - 1][k] * j / sushi
                if k != 0:
                    dp[i][j][k] += dp[i][j + 1][k - 1] * k / sushi
                dp[i][j][k] += N / sushi
    print(dp[one][two][three])


if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    def rec(i, j, k):
        if dp[i][j][k] >= 0:
            return dp[i][j][k]
        if i == 0 and j == 0 and k == 0:
            return 0.0

        res = 0.0
        if i > 0:
            res += rec(i - 1, j, k) * i
        if j > 0:
            res += rec(i + 1, j - 1, k) * j
        if k > 0:
            res += rec(i, j + 1, k - 1) * k
        res += N
        res *= 1.0 / (i + j + k)
        dp[i][j][k] = res

        return dp[i][j][k]

    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[-1] * (N + 1) for _ in range(N + 1)] for _ in range(N + 1)]
    print(rec(one, two, three))


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]
    for sushi in range(1, N + 1):
        for k in range(sushi + 1):
            for j in range(0, sushi - k + 1):
                i = sushi - k - j
                if i != 0:
                    dp[i][j][k] += dp[i - 1][j][k] * i / sushi
                if j != 0:
                    dp[i][j][k] += dp[i + 1][j - 1][k] * j / sushi
                if k != 0:
                    dp[i][j][k] += dp[i][j + 1][k - 1] * k / sushi
                dp[i][j][k] += N / sushi
    print(dp[one][two][three])


if __name__ == "__main__":
    resolve()