Maximum Sum Decreasing Subsequence

Given an array of N positive integers. The task is to find the sum of maximum sum decreasing subsequence(MSDS) of the given array such that the integers in the subsequence are sorted in decreasing order.

Examples:

Input: arr[] = {5, 4, 100, 3, 2, 101, 1}
Output: 106
100 + 3 + 2 + 1 = 106

Input: arr[] = {10, 5, 4, 3}
Output: 22
10 + 5 + 4 + 3 = 22



This problem is a variation of Longest Decreasing Subsequence problem. The Optimal Substructure for the above problem will be:

Let arr[0..n-1] be the input array and MSDS[i] be the maximum sum of the MSDS ending at index i such that arr[i] is the last element of the MSDS.
Then, MSDS[i] can be written as:

MSDS[i] = a[i] + max( MSDS[j] ) where i > j > 0 and arr[j] > arr[i] or,
MSDS[i] = a[i], if no such j exists.

To find the MSDS for a given array, we need to return max(MSDS[i]) where n > i > 0.

Below is the implementation of the above approach:

C++

// CPP code to return the maximum sum
// of decreasing subsequence in arr[]
#include <bits/stdc++.h>
using namespace std;
  
// function to return the maximum
// sum of decreasing subsequence
// in arr[]
int maxSumDS(int arr[], int n)
{
    int i, j, max = 0;
    int MSDS[n];
  
    // Initialize msds values
    // for all indexes
    for (i = 0; i < n; i++)
        MSDS[i] = arr[i];
  
    // Compute maximum sum values
    // in bottom up manner
    for (i = 1; i < n; i++)
        for (j = 0; j < i; j++)
            if (arr[i] < arr[j] && MSDS[i] < MSDS[j] + arr[i])
                MSDS[i] = MSDS[j] + arr[i];
  
    // Pick maximum of all msds values
    for (i = 0; i < n; i++)
        if (max < MSDS[i])
            max = MSDS[i];
  
    return max;
}
  
// Drive Code
int main()
{
    int arr[] = { 5, 4, 100, 3, 2, 101, 1 };
      
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << "Sum of maximum sum decreasing subsequence is: "
         << maxSumDS(arr, n);
    return 0;
}

Java

// Java code to return the maximum sum
// of decreasing subsequence in arr[]
import java.io.*;
import java.lang.*;
  
class GfG {
      
    // function to return the maximum
    // sum of decreasing subsequence
    // in arr[]
    public static int maxSumDS(int arr[], int n)
    {
        int i, j, max = 0;
        int[] MSDS = new int[n];
      
        // Initialize msds values
        // for all indexes
        for (i = 0; i < n; i++)
            MSDS[i] = arr[i];
      
        // Compute maximum sum values
        // in bottom up manner
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] < arr[j] && 
                    MSDS[i] < MSDS[j] + arr[i])
                    MSDS[i] = MSDS[j] + arr[i];
      
        // Pick maximum of all msds values
        for (i = 0; i < n; i++)
            if (max < MSDS[i])
                max = MSDS[i];
      
        return max;
    }
      
    // Drive Code
    public static void main(String argc[])
    {
        int arr[] = { 5, 4, 100, 3, 2, 101, 1 };
          
        int n = 7;
      
        System.out.println("Sum of maximum sum"
               + " decreasing subsequence is: "
                           + maxSumDS(arr, n));
    }
}
  
// This code os contributed by Sagar Shukla.

C#

// C# code to return the
// maximum sum of decreasing
// subsequence in arr[]
using System;
  
class GFG
{
      
    // function to return the 
    // maximum sum of decreasing
    // subsequence in arr[]
    public static int maxSumDS(int []arr, 
                               int n)
    {
        int i, j, max = 0;
        int[] MSDS = new int[n];
      
        // Initialize msds values
        // for all indexes
        for (i = 0; i < n; i++)
            MSDS[i] = arr[i];
      
        // Compute maximum sum values
        // in bottom up manner
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] < arr[j] && 
                    MSDS[i] < MSDS[j] + arr[i])
                    MSDS[i] = MSDS[j] + arr[i];
      
        // Pick maximum of 
        // all msds values
        for (i = 0; i < n; i++)
            if (max < MSDS[i])
                max = MSDS[i];
      
        return max;
    }
      
    // Drive Code
    static public void Main ()
    {
        int []arr = {5, 4, 100, 
                     3, 2, 101, 1};
        int n = 7;
        Console.WriteLine("Sum of maximum sum"
                " decreasing subsequence is: "
                              maxSumDS(arr, n));
    }
}
  
// This code is contributed by m_kit

PHP

<?php
// PHP code to return the maximum sum
// of decreasing subsequence in arr[]
  
// function to return the maximum
// sum of decreasing subsequence
// in arr[]
function maxSumDS($arr, $n)
{
    $i; $j; $max = 0;
    $MSDS = array();
  
    // Initialize msds values
    // for all indexes
    for ($i = 0; $i < $n; $i++)
        $MSDS[$i] = $arr[$i];
  
    // Compute maximum sum values
    // in bottom up manner
    for ($i = 1; $i < $n; $i++)
        for ($j = 0; $j < $i; $j++)
            if ($arr[$i] < $arr[$j] && 
                   $MSDS[$i] < $MSDS[$j] + $arr[$i])
                $MSDS[$i] = $MSDS[$j] + $arr[$i];
  
    // Pick maximum of 
    // all msds values
    for ($i = 0; $i < $n; $i++)
        if ($max < $MSDS[$i])
            $max = $MSDS[$i];
  
    return $max;
}
  
// Driver Code
$arr = array (5, 4, 100, 
              3, 2, 101, 1 );
  
$n = sizeof($arr);
  
echo "Sum of maximum sum decreasing "
                    "subsequence is: "
                    maxSumDS($arr, $n);
  
// This code is contributed by ajit
?>


Output:

Sum of maximum sum decreasing subsequence is: 106

Time complexity: O(N2)
Auxiliary Space: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t