Ignore the SC and understand the logic.
There are only 2 ways\n1. Take :\n\tif you choose the top element there are again two possibilities \n\t1. take current element and move to next list or \n\t2. 
There are only 2 ways\n1. Take :\n\tif you choose the top element there are again two possibilities \n\t1. take current element and move to next list or \n\t2. take curr element and stand on the same list and call recursion for the next element in the same list.\n2. Not Take :\n\tjust simply move to next list.\n```\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int max=0;\n        for(List<Integer> curr: piles){\n            max=Math.max(max,curr.size());\n        }\n        Integer dp[][][]=new Integer[piles.size()][max][k+1];\n        return f(piles,piles.size()-1,0,k,dp);\n    }\n    \n    public int f(List<List<Integer>> piles, int n, int i, int k,Integer dp[][][]){\n        if(n==-1 || i>=piles.get(n).size() || k<=0){\n            return 0;\n        }\n    \n        if(dp[n][i][k]!=null) return dp[n][i][k];\n        int take=0;\n        if(i<piles.get(n).size()){\n            \n            take=piles.get(n).get(i)+ Math.max(f(piles,n,i+1,k-1,dp),f(piles,n-1,0,k-1,dp));\n        }\n        int notTake=f(piles,n-1,0,k,dp);\n        return dp[n][i][k]=Math.max(take,notTake);\n    }\n}\n```
['Dynamic Programming', 'Memoization', 'Java']