logo CodeStepByStep logo

maxSortedSubsequenceSum

Language/Type: C++ dynamic programming

Write a function named maxSortedSubsequenceSum that accepts a reference to a vector of integers and returns the maximum sum of any non-decreasing subsequence of values in the vector, found using dynamic programming. A subsequence contains values in the same order they occurred in the original vector, though not necessarily consecutively. For example, if the vector stores {10, 22, 9, 33, 21, 50, 41, 60, 80}, the max-sum sorted subsequence is {10, 22, 33, 50, 60, 80}, so the value returned should be its sum, which is 255. If the vector stores {40, 10, 20, 5}, the max-sum sorted subsequence is {40}, so the value returned should be its sum of 40.

The key constraint of this problem is that you must solve it using a bottom-up dynamic programming approach. Do not use recursion. Your solution must use dynamic programming instead. You are allowed to construct any data structures (array, vector, set, map, etc.) necessary to store the data for your dynamic programming algorithm. You may assume that none of the values in the vector are negative.

Note that this problem is similar to longestSortedSubsequence, so you may want to refer to that problem.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.