HTML & JS
/* Dynamic Programming solution to construct
Maximum Sum Increasing Subsequence */
#include <iostream>
#include <vector>
using namespace std;

// Utility function to print Maximum Sum Increasing Subsequence
void printMaxSumIS(vector<int>& arr)
{
    for(int i : arr)
		cout << i << " ";

    cout << endl;
}

// Function to construct Maximum Sum Increasing Subsequence
vector<int> constructMaxSumIS(int arr[], int n)
{
    // L[i] stores the Maximum Sum Increasing Subsequence that
    // ends with arr[i]
    vector<vector<int>> L(n);

	// msis[i] stores the Maximum Sum of Increasing Subsequence that
    // ends with arr[i]
    vector<int> msis(n);
	
    // L[0] is equal to arr[0]
    L[0].push_back(arr[0]);
	msis[0] = arr[0];

	// start from index 1
    for(int i = 1; i < n; i++)
	{
		// for every j less than i
		for(int j = 0; j < i; j++)
		{
			/* L[i] = {MaxSum(L[j])} + arr[i]
			where j < i and arr[j] < arr[i] */
			if((arr[i] > arr[j]) && (msis[i] < msis[j]))
			{
				L[i] = L[j];
				msis[i] = msis[j];
			}
		}
		// L[i] ends with arr[i]
		L[i].push_back(arr[i]);	
		msis[i] += arr[i];

		// L[i] now stores Maximum Sum Increasing Subsequence of arr[0..i]
        // that ends with arr[i]
    }

    int maxIndex = 0;
	int max = msis[0];

	// find max
	for(int i = 1; i < n; i++)
		if(msis[i] > max)
		{
			max = msis[i];
			maxIndex = i;
		}

	// max will contain result
    return L[maxIndex];
}

// Driver function
int main()
{
    int arr[] = { 1, 101, 2, 3, 100, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);

	// construct and print Max Sum Inc Subseq of arr
    vector<int> msis = constructMaxSumIS(arr, n);
    printMaxSumIS(msis);

    return 0;
}

Generated URL:

Output:

1 2 3 100 
Report Bug