<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>, and an integer <code>target</code>.</p>

<p>Return <em>the <strong>length of the longest subsequence</strong> of</em> <code>nums</code> <em>that sums up to</em> <code>target</code>. <em>If no such subsequence exists, return</em> <code>-1</code>.</p>

<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], target = 9
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,1,3,2,1,5], target = 7
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,5,4,5], target = 3
<strong>Output:</strong> -1
<strong>Explanation:</strong> It can be shown that nums has no subsequence that sums up to 3.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>1 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution:
    def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
        

        #so for a given index i, if i come in with the same sum multiple times, i may as well cache the first time.
        #so that way i don't have to repeat work.
        #so i come in and what do i do? I guess I just return the longest subsequence that starts at an index for some given sum?
        # So when I come into 5, with either [1, 3] or [4], I'm looking for 5 more to sum to target, and the longest when I come into 5, is a length of 1. So maybe I can return that length to the previous function call. There, I get whatever the max is possible, and add 1 to indicate this number is included.
        #Then I return whatever is the longest I can make from the current index

        #So for a given sum and given index, return whatever the longest subsequence I can make starting at this index that is equal to target - given sum

        dp = [[None for i in range(len(nums))] for h in range(target+1)]

        def recur(given_sum, idx):
            if given_sum == target:
                return 0
            if ((target - given_sum) < 0) or (idx >= len(nums)):
                return -1
            if (dp[given_sum][idx] != None):
                return dp[given_sum][idx]
            
            included = recur(given_sum + nums[idx], idx+1)
            included = (included + 1) if (included != -1) else -1
            excluded = recur(given_sum, idx+1)

            maxnum = max(included, excluded)
            dp[given_sum][idx] = maxnum
            return dp[given_sum][idx]

        return recur(0, 0)




6655