<s>[INST] def matrixChain(order):
    n = len(order)
    dp = [[0]*n for _ in range(n)]
    
    for L in range(1, n):
        for i in range(n-L):
            j = i + L
            dp[i][j] = float('inf')
            for k in range(i, j):
                q = dp[i][k] + dp[k+1][j] + order[i]*order[k+1]*order[j+1]
                if q < dp[i][j]:
                    dp[i][j] = q
    return dp[0][n-1][/INST]