Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined
dp [ i + 1 , j ] = Math . Max ( dp [ i + 1 , j ] , stacks [ i , k ] + dp [ i , j - k ] ) ; } } }