Problem:


Score : 100 points

Problem Statement

There are N dishes, numbered 1, 2, \ldots, N.
Initially, for each i (1 \leq i \leq N), Dish i has a_i (1 \leq a_i \leq 3) pieces of sushi on it.

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

Constraints

All values in input are integers.

1 \leq N \leq 300

1 \leq a_i \leq 3

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten.
The output is considered correct when the relative difference is not greater than 10^{-9}.

Sample Input 1

3
1 1 1

Sample Output 1

5.5

The expected number of operations before the first piece of sushi is eaten, is 1.
After that, the expected number of operations before the second sushi is eaten, is 1.5.
After that, the expected number of operations before the third sushi is eaten, is 3.
Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.

Sample Input 2

1
3

Sample Output 2

3

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.

Sample Input 3

2
1 2

Sample Output 3

4.5

Sample Input 4

10
1 3 2 3 3 2 3 2 1 3

Sample Output 4

54.48064457488221



Sample input:
3
1 1 1


Sample output:
5.5

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-2):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        dp[i+2] = min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    dp[n-1] = min(dp[n-1],dp[n-2]+abs(h[n-1]-h[n-2]))
    print(dp[n-1])
#B
def B():
    n,k = LI()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(1,min(n-i,k+1)):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
    print(dp[n-1])

#C
def C():
    n = I()
    v = LIR(n)
    dp = [[0,0,0] for i in range(n+1)]
    for i in range(n):
        dp[i+1][0] = max(dp[i+1][0],dp[i][1]+v[i][0],dp[i][2]+v[i][0])
        dp[i+1][1] = max(dp[i+1][1],dp[i][2]+v[i][1],dp[i][0]+v[i][1])
        dp[i+1][2] = max(dp[i+1][2],dp[i][0]+v[i][2],dp[i][1]+v[i][2])
    print(max(dp[n]))

#D
def D():
    n,W = LI()
    g = LIR(n)
    dp = [[0 for i in range(W+1)] for j in range(n+1)]
    for i in range(n):
        for j in range(W+1)[::-1]:
            w,v = g[i]
            if j >= w:
                dp[i+1][j] = max(dp[i][j],dp[i][j-w]+v)
            else:
                dp[i+1][j] = max(dp[i+1][j],dp[i][j])
    print(max(dp[n]))
#E
def E():
    n,W = LI()
    g = LIR(n)
    V = 100000
    dp = [[float("inf") for i in range(V+1)] for j in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(V+1)[::-1]:
            w,v = g[i]
            if j >= v:
                dp[i+1][j] = min(dp[i][j],dp[i][j-v]+w)
            else:
                dp[i+1][j] = min(dp[i+1][j],dp[i][j])

    for i in range(V+1)[::-1]:
        if dp[n][i] <= W:
            print(i)
            quit()

#F
def F():
    s = S()
    t = S()
    dp = [[0 for i in range(len(t)+1)] for j in range(len(s)+1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i] == t[j]:
                dp[i+1][j+1] = max(dp[i][j]+1,dp[i+1][j],dp[i][j+1])
            else:
                dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1])

    i = len(s)
    j = len(t)
    ans = ""
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:i -= 1
        elif dp[i][j] == dp[i][j-1]:j -= 1
        else:
            i -= 1
            j -= 1
            ans = s[i]+ans
    print(ans)
#G
def G():
    def dfs(x):
        if dp[x] != None:
            return dp[x]
        if len(v[x]) == 0:
            dp[x] = 0
            return 0
        else:
            res = 0
            for y in v[x]:
                res = max(res,dfs(y)+1)
            dp[x] = res
            return dp[x]
    n,m = LI()
    dp = [None for i in range(n)]
    v = [[] for i in range(n)]
    for i in range(m):
        x,y = LI()
        v[x-1].append(y-1)
    for i in range(n):
        dfs(i)
    print(max(dp))

#H
def H():
    h,w = LI()
    s = SR(h)
    dp = [[0 for i in range(w)] for j in range(h)]
    dp[0][0] = 1
    for i in range(h-1):
        for j in range(w-1):
            if s[i+1][j] == ".":
                dp[i+1][j] += dp[i][j]
                dp[i+1][j] %= mod
            if s[i][j+1] == ".":
                dp[i][j+1] += dp[i][j]
                dp[i][j+1] %= mod
    for j in range(w-1):
        if s[h-1][j+1] == ".":
            dp[h-1][j+1] += dp[h-1][j]
            dp[h-1][j+1] %= mod
    for i in range(h-1):
        if s[i+1][w-1] == ".":
            dp[i+1][w-1] += dp[i][w-1]
            dp[i+1][w-1] %= mod
    print(dp[h-1][w-1])

#I
def J():
    n = I()
    p = list(map(float, input().split()))
    dp = [[0 for i in range(n+1)] for j in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        for j in range(n+1):
            dp[i+1][j] = p[i]*dp[i][j-1]+(1-p[i])*dp[i][j]

    ans = 0
    m = math.floor(n/2)
    for i in range(m+1,n+1):
        ans += dp[n][i]
    print(ans)

#J
def K():
    n = I()
    a = LI()
    d1 = a.count(1)
    d2 = a.count(2)
    d3 = a.count(3)
    dp = [[[0 for i in range(d3+1)] for j in range(d2+d3+1)] for k in range(n+1)]
    for k in range(d3+1):
        for j in range(d2+d3+1):
            for i in range(n+1):
                if i+j+k:
                    l = i+j+k
                    dp[i][j][k] = n/l
                    if i:
                        dp[i][j][k] += i/l*dp[i-1][j][k]
                    if j and i < n:
                        dp[i][j][k] += j/l*dp[i+1][j-1][k]
                    if k and j < d2+d3:
                        dp[i][j][k] += k/l*dp[i][j+1][k-1]

    print(dp[d1][d2][d3])
#Solve
if __name__ == "__main__":
    K()

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-2):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        dp[i+2] = min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    dp[n-1] = min(dp[n-1],dp[n-2]+abs(h[n-1]-h[n-2]))
    print(dp[n-1])
#B
def B():
    n,k = LI()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(1,min(n-i,k+1)):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
    print(dp[n-1])

#C
def C():
    n = I()
    v = LIR(n)
    dp = [[0,0,0] for i in range(n+1)]
    for i in range(n):
        dp[i+1][0] = max(dp[i+1][0],dp[i][1]+v[i][0],dp[i][2]+v[i][0])
        dp[i+1][1] = max(dp[i+1][1],dp[i][2]+v[i][1],dp[i][0]+v[i][1])
        dp[i+1][2] = max(dp[i+1][2],dp[i][0]+v[i][2],dp[i][1]+v[i][2])
    print(max(dp[n]))

#D
def D():
    n,W = LI()
    g = LIR(n)
    dp = [[0 for i in range(W+1)] for j in range(n+1)]
    for i in range(n):
        for j in range(W+1)[::-1]:
            w,v = g[i]
            if j >= w:
                dp[i+1][j] = max(dp[i][j],dp[i][j-w]+v)
            else:
                dp[i+1][j] = max(dp[i+1][j],dp[i][j])
    print(max(dp[n]))
#E
def E():
    n,W = LI()
    g = LIR(n)
    V = 100000
    dp = [[float("inf") for i in range(V+1)] for j in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(V+1)[::-1]:
            w,v = g[i]
            if j >= v:
                dp[i+1][j] = min(dp[i][j],dp[i][j-v]+w)
            else:
                dp[i+1][j] = min(dp[i+1][j],dp[i][j])

    for i in range(V+1)[::-1]:
        if dp[n][i] <= W:
            print(i)
            quit()

#F
def F():
    s = S()
    t = S()
    dp = [[0 for i in range(len(t)+1)] for j in range(len(s)+1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i] == t[j]:
                dp[i+1][j+1] = max(dp[i][j]+1,dp[i+1][j],dp[i][j+1])
            else:
                dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1])

    i = len(s)
    j = len(t)
    ans = ""
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:i -= 1
        elif dp[i][j] == dp[i][j-1]:j -= 1
        else:
            i -= 1
            j -= 1
            ans = s[i]+ans
    print(ans)
#G
def G():
    def dfs(x):
        if dp[x] != None:
            return dp[x]
        if len(v[x]) == 0:
            dp[x] = 0
            return 0
        else:
            res = 0
            for y in v[x]:
                res = max(res,dfs(y)+1)
            dp[x] = res
            return dp[x]
    n,m = LI()
    dp = [None for i in range(n)]
    v = [[] for i in range(n)]
    for i in range(m):
        x,y = LI()
        v[x-1].append(y-1)
    for i in range(n):
        dfs(i)
    print(max(dp))

#H
def H():
    h,w = LI()
    s = SR(h)
    dp = [[0 for i in range(w)] for j in range(h)]
    dp[0][0] = 1
    for i in range(h-1):
        for j in range(w-1):
            if s[i+1][j] == ".":
                dp[i+1][j] += dp[i][j]
                dp[i+1][j] %= mod
            if s[i][j+1] == ".":
                dp[i][j+1] += dp[i][j]
                dp[i][j+1] %= mod
    for j in range(w-1):
        if s[h-1][j+1] == ".":
            dp[h-1][j+1] += dp[h-1][j]
            dp[h-1][j+1] %= mod
    for i in range(h-1):
        if s[i+1][w-1] == ".":
            dp[i+1][w-1] += dp[i][w-1]
            dp[i+1][w-1] %= mod
    print(dp[h-1][w-1])

#I
def J():
    n = I()
    p = list(map(float, input().split()))
    dp = [[0 for i in range(n+1)] for j in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        for j in range(n+1):
            dp[i+1][j] = p[i]*dp[i][j-1]+(1-p[i])*dp[i][j]

    ans = 0
    m = math.floor(n/2)
    for i in range(m+1,n+1):
        ans += dp[n][i]
    print(ans)

#J
def K():
    def dfs(i,j,k):
        if dp[i][j][k] != None:
            return dp[i][j][k]
        l = i+j+k
        res = n/l
        if i:
            res += i/l*dfs(i-1,j,k)
        if j:
            res += j/l*dfs(i+1,j-1,k)
        if k:
            res += k/l*dfs(i,j+1,k-1)
        dp[i][j][k] = res
        return dp[i][j][k]
    n = I()
    a = LI()
    d1 = a.count(1)
    d2 = a.count(2)
    d3 = a.count(3)
    dp = [[[None for i in range(d3+1)] for j in range(d2+d3+1)] for k in range(n+1)]
    dp[0][0][0] = 0
    dfs(d1,d2,d3)
    print(dp[d1][d2][d3])
#Solve
if __name__ == "__main__":
    K()

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-2):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        dp[i+2] = min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    dp[n-1] = min(dp[n-1],dp[n-2]+abs(h[n-1]-h[n-2]))
    print(dp[n-1])
#B
def B():
    n,k = LI()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(1,min(n-i,k+1)):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
    print(dp[n-1])

#C
def C():
    n = I()
    v = LIR(n)
    dp = [[0,0,0] for i in range(n+1)]
    for i in range(n):
        dp[i+1][0] = max(dp[i+1][0],dp[i][1]+v[i][0],dp[i][2]+v[i][0])
        dp[i+1][1] = max(dp[i+1][1],dp[i][2]+v[i][1],dp[i][0]+v[i][1])
        dp[i+1][2] = max(dp[i+1][2],dp[i][0]+v[i][2],dp[i][1]+v[i][2])
    print(max(dp[n]))

#D
def D():
    n,W = LI()
    g = LIR(n)
    dp = [[0 for i in range(W+1)] for j in range(n+1)]
    for i in range(n):
        for j in range(W+1)[::-1]:
            w,v = g[i]
            if j >= w:
                dp[i+1][j] = max(dp[i][j],dp[i][j-w]+v)
            else:
                dp[i+1][j] = max(dp[i+1][j],dp[i][j])
    print(max(dp[n]))
#E
def E():
    n,W = LI()
    g = LIR(n)
    V = 100000
    dp = [[float("inf") for i in range(V+1)] for j in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(V+1)[::-1]:
            w,v = g[i]
            if j >= v:
                dp[i+1][j] = min(dp[i][j],dp[i][j-v]+w)
            else:
                dp[i+1][j] = min(dp[i+1][j],dp[i][j])

    for i in range(V+1)[::-1]:
        if dp[n][i] <= W:
            print(i)
            quit()

#F
def F():
    s = S()
    t = S()
    dp = [[0 for i in range(len(t)+1)] for j in range(len(s)+1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i] == t[j]:
                dp[i+1][j+1] = max(dp[i][j]+1,dp[i+1][j],dp[i][j+1])
            else:
                dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1])

    i = len(s)
    j = len(t)
    ans = ""
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:i -= 1
        elif dp[i][j] == dp[i][j-1]:j -= 1
        else:
            i -= 1
            j -= 1
            ans = s[i]+ans
    print(ans)
#G
def G():
    def dfs(x):
        if dp[x] != None:
            return dp[x]
        if len(v[x]) == 0:
            dp[x] = 0
            return 0
        else:
            res = 0
            for y in v[x]:
                res = max(res,dfs(y)+1)
            dp[x] = res
            return dp[x]
    n,m = LI()
    dp = [None for i in range(n)]
    v = [[] for i in range(n)]
    for i in range(m):
        x,y = LI()
        v[x-1].append(y-1)
    for i in range(n):
        dfs(i)
    print(max(dp))

#H
def H():
    h,w = LI()
    s = SR(h)
    dp = [[0 for i in range(w)] for j in range(h)]
    dp[0][0] = 1
    for i in range(h-1):
        for j in range(w-1):
            if s[i+1][j] == ".":
                dp[i+1][j] += dp[i][j]
                dp[i+1][j] %= mod
            if s[i][j+1] == ".":
                dp[i][j+1] += dp[i][j]
                dp[i][j+1] %= mod
    for j in range(w-1):
        if s[h-1][j+1] == ".":
            dp[h-1][j+1] += dp[h-1][j]
            dp[h-1][j+1] %= mod
    for i in range(h-1):
        if s[i+1][w-1] == ".":
            dp[i+1][w-1] += dp[i][w-1]
            dp[i+1][w-1] %= mod
    print(dp[h-1][w-1])

#I
def J():
    n = I()
    p = list(map(float, input().split()))
    dp = [[0 for i in range(n+1)] for j in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        for j in range(n+1):
            dp[i+1][j] = p[i]*dp[i][j-1]+(1-p[i])*dp[i][j]

    ans = 0
    m = math.floor(n/2)
    for i in range(m+1,n+1):
        ans += dp[n][i]
    print(ans)

#J
def K():
    def dfs(i,j,k):
        if dp[i][j][k] != None:
            return dp[i][j][k]
        l = i+j+k
        res = n/l
        if i:
            res += i/l*dfs(i-1,j,k)
        if j:
            res += j/l*dfs(i+1,j-1,k)
        if k:
            res += k/l*dfs(i,j+1,k-1)
        dp[i][j][k] = res
        return dp[i][j][k]
    n = I()
    a = LI()
    d1 = a.count(1)
    d2 = a.count(2)
    d3 = a.count(3)
    dp = [[[None for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
    dp[0][0][0] = 0
    dfs(d1,d2,d3)
    print(dp[d1][d2][d3])
#Solve
if __name__ == "__main__":
    K()

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
sys.setrecursionlimit(1000000)

#A
def A():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-2):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        dp[i+2] = min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    dp[n-1] = min(dp[n-1],dp[n-2]+abs(h[n-1]-h[n-2]))
    print(dp[n-1])
#B
def B():
    n,k = LI()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(1,min(n-i,k+1)):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
    print(dp[n-1])

#C
def C():
    n = I()
    v = LIR(n)
    dp = [[0,0,0] for i in range(n+1)]
    for i in range(n):
        dp[i+1][0] = max(dp[i+1][0],dp[i][1]+v[i][0],dp[i][2]+v[i][0])
        dp[i+1][1] = max(dp[i+1][1],dp[i][2]+v[i][1],dp[i][0]+v[i][1])
        dp[i+1][2] = max(dp[i+1][2],dp[i][0]+v[i][2],dp[i][1]+v[i][2])
    print(max(dp[n]))

#D
def D():
    n,W = LI()
    g = LIR(n)
    dp = [[0 for i in range(W+1)] for j in range(n+1)]
    for i in range(n):
        for j in range(W+1)[::-1]:
            w,v = g[i]
            if j >= w:
                dp[i+1][j] = max(dp[i][j],dp[i][j-w]+v)
            else:
                dp[i+1][j] = max(dp[i+1][j],dp[i][j])
    print(max(dp[n]))
#E
def E():
    n,W = LI()
    g = LIR(n)
    V = 100000
    dp = [[float("inf") for i in range(V+1)] for j in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(V+1)[::-1]:
            w,v = g[i]
            if j >= v:
                dp[i+1][j] = min(dp[i][j],dp[i][j-v]+w)
            else:
                dp[i+1][j] = min(dp[i+1][j],dp[i][j])

    for i in range(V+1)[::-1]:
        if dp[n][i] <= W:
            print(i)
            quit()

#F
def F():
    s = S()
    t = S()
    dp = [[0 for i in range(len(t)+1)] for j in range(len(s)+1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i] == t[j]:
                dp[i+1][j+1] = max(dp[i][j]+1,dp[i+1][j],dp[i][j+1])
            else:
                dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1])

    i = len(s)
    j = len(t)
    ans = ""
    while i > 0 and j > 0:
        if dp[i][j] == dp[i-1][j]:i -= 1
        elif dp[i][j] == dp[i][j-1]:j -= 1
        else:
            i -= 1
            j -= 1
            ans = s[i]+ans
    print(ans)
#G
def G():
    def dfs(x):
        if dp[x] != None:
            return dp[x]
        if len(v[x]) == 0:
            dp[x] = 0
            return 0
        else:
            res = 0
            for y in v[x]:
                res = max(res,dfs(y)+1)
            dp[x] = res
            return dp[x]
    n,m = LI()
    dp = [None for i in range(n)]
    v = [[] for i in range(n)]
    for i in range(m):
        x,y = LI()
        v[x-1].append(y-1)
    for i in range(n):
        dfs(i)
    print(max(dp))

#H
def H():
    h,w = LI()
    s = SR(h)
    dp = [[0 for i in range(w)] for j in range(h)]
    dp[0][0] = 1
    for i in range(h-1):
        for j in range(w-1):
            if s[i+1][j] == ".":
                dp[i+1][j] += dp[i][j]
                dp[i+1][j] %= mod
            if s[i][j+1] == ".":
                dp[i][j+1] += dp[i][j]
                dp[i][j+1] %= mod
    for j in range(w-1):
        if s[h-1][j+1] == ".":
            dp[h-1][j+1] += dp[h-1][j]
            dp[h-1][j+1] %= mod
    for i in range(h-1):
        if s[i+1][w-1] == ".":
            dp[i+1][w-1] += dp[i][w-1]
            dp[i+1][w-1] %= mod
    print(dp[h-1][w-1])

#I
def J():
    n = I()
    p = list(map(float, input().split()))
    dp = [[0 for i in range(n+1)] for j in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        for j in range(n+1):
            dp[i+1][j] = p[i]*dp[i][j-1]+(1-p[i])*dp[i][j]

    ans = 0
    m = math.floor(n/2)
    for i in range(m+1,n+1):
        ans += dp[n][i]
    print(ans)

#J
def K():
    def dfs(i,j,k):
        if dp[i][j][k] != None:
            return dp[i][j][k]
        l = i+j+k
        res = n/l
        if i:
            res += i/l*dfs(i-1,j,k)
        if j:
            res += j/l*dfs(i+1,j-1,k)
        if k:
            res += k/l*dfs(i,j+1,k-1)
        dp[i][j][k] = res
        return dp[i][j][k]
    n = I()
    a = LI()
    s = sum(a)
    d = [0,0,0]
    for i in a:
        d[i-1] += 1
    d1,d2,d3 = d
    dp = [[[None for i in range(d1+d2+d3+1)] for j in range(d1+d2+d3+1)] for k in range(d1+d2+d3+1)]
    dp[0][0][0] = 0
    dfs(d1,d2,d3)
    print(dp[d1][d2][d3])
#Solve
if __name__ == "__main__":
    K()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline

def f(one, two, thr):
    thr_ = thr + 1
    two_ = n + 1 - one
    one_ = n + 1
    dp = [[[0] * (thr_) for _ in range(two_)] for _ in range(one_)]
    for k in range(thr_):
        for j in range(two_ - k):
            for i in range(one_ - k - j):
                r = i + j + k
                dp_ = 0
                if r:
                    dp_ = n
                    dp_ += 0 if not i else i * dp[i - 1][j][k]
                    dp_ += 0 if not j else j * dp[i + 1][j - 1][k]
                    dp_ += 0 if not k else k * dp[i][j + 1][k - 1]
                    dp_ /= r
                dp[i][j][k] = dp_
    return(dp[one][two][thr])

n = int(input()) # n: odd, 1 <= n <= 30
ai = input().split()
a = [ai.count(x) for x in ["1", "2", "3"]]
print(f(*a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from functools import lru_cache
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

@lru_cache(maxsize = None)
def dfs(i, j, k):
    r = i + j + k
    if not r:
        return(0)
    dfs_ = n
    dfs_ += 0 if not i else i * dfs(i - 1, j, k)
    dfs_ += 0 if not j else j * dfs(i + 1, j - 1, k)
    dfs_ += 0 if not k else k * dfs(i, j + 1, k - 1)
    return(dfs_ / r)

n = int(input()) # n: odd, 1 <= n <= 300
ai = [0, 0, 0]
for a in map(int, input().split()):
    ai[a - 1] += 1
print(dfs(*ai))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

from functools import lru_cache
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

@lru_cache(maxsize = None)
def dfs(i, j, k):
    r = i + j + k
    if not r:
        return(0)
    dfs_ = n
    if i:
        dfs_ += i * dfs(i - 1, j, k)
    if j:
        dfs_ += j * dfs(i + 1, j - 1, k)
    if k:
        dfs_ += k * dfs(i, j + 1, k - 1)
    return(dfs_ / r)

n = int(input()) # n: odd, 1 <= n <= 300
ai = [0, 0, 0]
for a in map(int, input().split()):
    ai[a - 1] += 1
print(dfs(*ai))

Submission status: Time Limit Exceeded

Solution code:
def makelist(n, m, o):
    return [[[-1 for _ in range(o)] for _ in range(m)] for _ in range(n)]


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

cnt = [0]*4
for e in a:
    cnt[e] += 1

dp = makelist(N+1, N+1, N+1)
dp[0][0][0] = 0

def calc(i1, i2, i3):
    if dp[i1][i2][i3] != -1:
        return dp[i1][i2][i3]
    else:
        i0 = N - (i1 + i2 + i3)
        numer = \
            N \
            + ((i1 * calc(i1-1, i2, i3)) if i1 else 0) \
            + ((i2 * calc(i1+1, i2-1, i3)) if i2 else 0) \
            + ((i3 * calc(i1, i2+1, i3-1) if i3 else 0))
        denom = i1 + i2 + i3

        res = numer / denom
        dp[i1][i2][i3] = res
        return res

print(calc(cnt[1], cnt[2], cnt[3]))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from collections import Counter
input = sys.stdin.readline
n=int(input())
a = tuple(map(int,input().split()))

c = 0
n = 10**7
for i in range(2*n):
  c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from collections import Counter
input = sys.stdin.readline
n=int(input())
a = tuple(map(int,input().split()))

c = 0
n = 10**8
for i in range(n):
  c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from collections import Counter
input = sys.stdin.readline
n=int(input())
a = tuple(map(int,input().split()))
cnt = Counter(a)
    
def f(i,j,k):
    if [i,j,k] == [0,0,0]:
        return 0.0
    if dp[i][j][k] != 0.0:
        return dp[i][j][k]
    tmp = 0.0
    if i>0:
        tmp += f(i-1,j,k)*(i)/(i+j+k)
    if j>0:
        tmp += f(i+1,j-1,k)*(j)/(i+j+k)
    if k>0:
        tmp += f(i,j+1,k-1)*k/(i+j+k)
    tmp += (n)/(i+j+k)
    dp[i][j][k] = tmp
    return tmp
dp = np.zeros((n+1,n+1,n+1))
#dp = [[[0.0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
 
print(f(cnt[1],cnt[2],cnt[3]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
n=int(input())
a = tuple(map(int,input().split()))
cnt = [0,0,0] #1個の個数、二個の個数、三個の個数
for i in range(n):
    cnt[a[i]-1] += 1
    
def f(i,j,k):
    if [i,j,k] == [0,0,0]:
        return 0.0
    if dp[i][j][k] != 0.0:
        return dp[i][j][k]
    tmp = 0.0
    if i>0:
        tmp += f(i-1,j,k)*float(i)/float(i+j+k)
    if j>0:
        tmp += f(i+1,j-1,k)*float(j)/float(i+j+k)
    if k>0:
        tmp += f(i,j+1,k-1)*float(k)/float(i+j+k)
    tmp += float(n)/float(i+j+k)
    
 
    dp[i][j][k] = tmp
    return tmp
dp = np.zeros((n+1,n+1,n+1))
#dp = [[[0.0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
 
print(f(cnt[0],cnt[1],cnt[2]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a = list(map(int,input().split()))
cnt = [0,0,0] #1個の個数、二個の個数、三個の個数
for i in range(n):
    cnt[a[i]-1] += 1
    
def f(i,j,k):
    if [i,j,k] == [0,0,0]:
        return 0.0
    if dp[i][j][k] != 0.0:
        return dp[i][j][k]
    tmp = 0.0
    if i>0:
        tmp += f(i-1,j,k)*float(i)/float(i+j+k)
    if j>0:
        tmp += f(i+1,j-1,k)*float(j)/float(i+j+k)
    if k>0:
        tmp += f(i,j+1,k-1)*float(k)/float(i+j+k)
    tmp += float(n)/float(i+j+k)
    
 
    dp[i][j][k] = tmp
    return tmp

dp = [[[0.0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
 
print(f(cnt[0],cnt[1],cnt[2]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 17:22:36 2019
DP_J
@author: maezawa
"""

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    dp = [[[-1]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
    
    def rec(i, j, k):
        if dp[i][j][k] > -1:
            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 = (res + n)/(i+j+k)
        dp[i][j][k] = res
        return res
    
    one = 0
    two = 0
    three = 0
    for i in a:
        if i == 1:
            one += 1
        elif i == 2:
            two += 1
        elif i == 3:
            three += 1
    print(rec(one, two, three))
        
main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 17:22:36 2019
DP_J
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))

dp = [[[-1]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

def rec(i, j, k):
    if dp[i][j][k] > -1:
        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 = (res + n)/(i+j+k)
    dp[i][j][k] = res
    return res

one = 0
two = 0
three = 0
for i in a:
    if i == 1:
        one += 1
    elif i == 2:
        two += 1
    elif i == 3:
        three += 1
print(rec(one, two, three))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 17:22:36 2019
DP_J
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))

dp = [[[-1]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

def rec(i, j, k):
    if dp[i][j][k] > -1:
        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 res

one = 0
two = 0
three = 0
for i in a:
    if i == 1:
        one += 1
    elif i == 2:
        two += 1
    elif i == 3:
        three += 1
print(rec(one, two, three))

Submission status: Time Limit Exceeded

Solution code:
import sys 
input = sys.stdin.readline
def dfs(one,two,three):
    if memo[one][two][three] != -1:
        return memo[one][two][three]
    ret = 0
    if one > 0:
        ret += (dfs(one-1,two,three)*one)
    if two > 0:
        ret += (dfs(one+1,two-1,three)*two)
    if three > 0:
        ret += (dfs(one,two+1,three-1)*three)
    ret += N
    ret /= (one+two+three)
    memo[one][two][three] = ret
    return ret

if __name__ == '__main__':
    N = int(input())
    a = list(map(int,input().split(" ")))
    memo = [ [ [-1]*(N+1)  for i in range(N+1)] for j in range(N+1) ]
    memo[0][0][0] = 0
    one = a.count(1)
    two = a.count(2)
    three = a.count(3)
    print(dfs(one,two,three))