Remove array end element to maximize the sum of product | C # 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 code
using System ; class GFG { static int MAX = 50 ; static int solve ( int [ , ] dp , 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 ] = Math . 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 ] ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 3 , 1 , 5 , 2 } ; int n = arr . Length ; int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = 0 ; Console . Write ( solve ( dp , arr , 0 , n - 1 , 1 ) ) ; } }