Maximum Sum Increasing Subsequence
Maximum Sum Increasing Subsequence is a GFG problem (Medium). Given an array arr of positive integers, pick some elements that keep their original order and get strictly larger from left to right. Return the largest possible sum of the picked elements.
- The picked elements do not have to sit next to each other.
- Strictly larger means two equal values can never both be picked.
The site lists it as Max Sum Increasing Subsequence. With at most 1,000 elements, checking every pair of positions (O(n²)) is fast enough.
- 1 <= arr.length <= 10³
- 1 <= arr[i] <= 10⁵
Intuition
Let dp[i] be the largest sum of an increasing subsequence that ends at arr[i]. Any such subsequence is either arr[i] alone, or an increasing subsequence that ends at some earlier, smaller value arr[j], with arr[i] added on the end. So:
dp[i] = arr[i] + the largest dp[j] among earlier positions with arr[j] < arr[i], or just arr[i] when no earlier value is smaller.
This is the same shape as the longest increasing subsequence recurrence, with sums in place of lengths. The answer is the largest dp[i], since the best subsequence can end at any position.
"Pick elements in order, each larger than the last, and optimise a total" is a pick-in-order DP over the ending position. Lengths give the longest increasing subsequence; sums give this problem.
Approach
Before reading on, find the answer for [1, 101, 2, 3, 100] by hand and check whether the longest increasing run you can pick is also the one with the biggest sum. Aim for O(n²).
Start each sum at its own value
Set dp = arr[:]. A single element is always an increasing subsequence, so dp[i] can never be smaller than arr[i].
Look back at every earlier element
For each i, loop j from 0 to i - 1. When arr[j] < arr[i], set dp[i] = max(dp[i], dp[j] + arr[i]), because putting arr[i] after a smaller value keeps the subsequence increasing.
Why one number per position is enough
To extend a subsequence later, you only need its last value (to check the next one is larger) and its sum. Among all subsequences that end at arr[i], the one with the largest sum is always the best one to extend, so dp[i] loses nothing by keeping only that sum.
Return the largest sum
Return max(dp). The best subsequence may stop before the last element, so the answer is the largest value anywhere in dp.
Code in Python, C++ and Java
Common pitfalls
Allowing equal values
if arr[j] <= arr[i]:
if arr[j] < arr[i]:
The subsequence must be strictly increasing. On [5, 5, 6] the <= version picks both 5s and returns 16, but the answer is 11 (one 5 and the 6).
Returning the last value of dp
return dp[-1]
return max(dp)
dp[-1] is only the best subsequence that ends at the last element. On [3, 4, 1] it returns 1, while [3, 4] sums to 7.
Edge cases
[10, 5, 4, 3]No earlier value is ever smaller, so every dp[i] stays at arr[i] and the answer is the largest single element, 10.
Complexity
dp array holds one sum per element.Maximum Sum Increasing Subsequence FAQ
Is the longest increasing subsequence also the one with the largest sum?
Not always. In [1, 101, 2, 3, 100, 4, 5] the longest increasing subsequence is 1, 2, 3, 4, 5, with sum 15. The best sum is 1, 2, 3, 100 with 106: it is shorter, but 100 outweighs 4 and 5 together. That is why the recurrence has to track sums, not lengths.
How do I print the subsequence, not just its sum?
Keep a parent array. Whenever dp[i] improves through j, set parent[i] = j. At the end, start from the index with the largest dp and follow parent back until it is empty, then reverse the list you collected.
Can it be done faster than O(n²)?
Yes, in O(n log n). Process the elements left to right and keep, for every value seen so far, the best sum ending at that value in a structure that answers "largest sum among values below x" quickly: a Fenwick tree over the sorted distinct values, or an ordered map. For 1,000 elements the plain double loop is already fast.