Remove array end element to maximize the sum of product | CPP program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  50 NEW_LINE using namespace std ; int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) return a [ low ] * turn ; if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ; dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int dp [ MAX ] [ MAX ] ; memset ( dp , 0 , sizeof ( dp ) ) ; cout << solve ( dp , arr , 0 , n - 1 , 1 ) << endl ; return 0 ; }