GeeksforGeeks Medium

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.

Constraints
  • 1 <= arr.length <= 10³
  • 1 <= arr[i] <= 10⁵
dpsubsequencearray
Open on GeeksforGeeks ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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²).

1

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].

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxSumIS(self, arr):
▶3 dp = arr[:]
▶4 for i in range(1, len(arr)):
▶5 for j in range(i):
▶6 if arr[j] < arr[i]:
▶7 dp[i] = max(dp[i], dp[j] + arr[i])
▶8 return max(dp)
arrbestsum111011012233100100each value alone is a subsequence
dp[1, 101, 2, 3, 100]copy of arr
Before any comparison, the best increasing subsequence ending at each element is the element on its own, so every sum starts at its own value.
arrbestsum1110110222331001001<1011+101=102best sum therenew bestsmaller: can extendafter 1: 102 → new best for 101
arr[0] vs arr[1]1 vs 101smaller
dp[1]102was 101
1 < 101, so 101 can follow the best subsequence ending at 1 (sum 1). That gives 102, more than the 101 found so far.
arrbestsum1110110223331001001<21+2=3best sum therenew bestsmaller: can extendafter 1: 3 → new best for 2
arr[0] vs arr[2]1 vs 2smaller
dp[2]3was 2
1 < 2, so 2 can follow the best subsequence ending at 1 (sum 1). That gives 3, more than the 2 found so far.
arrbestsum111011022333100100101>2not smaller101 is not smaller than 2 → skip
arr[1] vs arr[2]101 vs 2not smaller
dp[2]3unchanged
101 is bigger than 2, so putting 2 after it would go downhill. This pair is skipped.
arrbestsum1110110223341001001<31+3=4best sum therenew bestsmaller: can extendafter 1: 4 → new best for 3
arr[0] vs arr[3]1 vs 3smaller
dp[3]4was 3
1 < 3, so 3 can follow the best subsequence ending at 1 (sum 1). That gives 4, more than the 3 found so far.
arrbestsum111011022334100100101>3not smaller101 is not smaller than 3 → skip
arr[1] vs arr[3]101 vs 3not smaller
dp[3]4unchanged
101 is bigger than 3, so putting 3 after it would go downhill. This pair is skipped.
arrbestsum1110110223361001002<33+3=6best sum therenew bestsmaller: can extendafter 2: 6 → new best for 3
arr[2] vs arr[3]2 vs 3smaller
dp[3]6was 4
2 < 3, so 3 can follow the best subsequence ending at 2 (sum 3). That gives 6, more than the 4 found so far.
arrbestsum1110110223361001011<1001+100=101best sum therenew bestsmaller: can extendafter 1: 101 → new best for 100
arr[0] vs arr[4]1 vs 100smaller
dp[4]101was 100
1 < 100, so 100 can follow the best subsequence ending at 1 (sum 1). That gives 101, more than the 100 found so far.
arrbestsum111011022336100101101>100not smaller101 is not smaller than 100 → skip
arr[1] vs arr[4]101 vs 100not smaller
dp[4]101unchanged
101 is bigger than 100, so putting 100 after it would go downhill. This pair is skipped.
arrbestsum1110110223361001032<1003+100=103best sum therenew bestsmaller: can extendafter 2: 103 → new best for 100
arr[2] vs arr[4]2 vs 100smaller
dp[4]103was 101
2 < 100, so 100 can follow the best subsequence ending at 2 (sum 3). That gives 103, more than the 101 found so far.
arrbestsum1110110223361001063<1006+100=106best sum therenew bestsmaller: can extendafter 3: 106 → new best for 100
arr[3] vs arr[4]3 vs 100smaller
dp[4]106was 103
3 < 100, so 100 can follow the best subsequence ending at 3 (sum 6). That gives 106, more than the 103 found so far.
arrbestsum111011022336100106answer: 106
max(dp)106
The largest sum in the bottom row is 106, ending at 100. Following each improvement back gives the picked values in green: 1, 2, 3, 100.
05

Common pitfalls

Allowing equal values

✗ Wrong
if arr[j] <= arr[i]:
✓ Right
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

✗ Wrong
return dp[-1]
✓ Right
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.

06

Edge cases

Strictly decreasing array, such as [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.

07

Complexity

Time
O(n²)
Space
O(n)
Each position compares itself with every earlier position, which is n(n − 1)/2 checks. The dp array holds one sum per element.
08

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.