Minimum possible sum of prices of a Triplet from the given Array | C # program to implement the above approach ; Function to minimize the sum of price by taking a triplet ; Initialize a [ ] dp array ; Stores the readonly result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver Code
using System ; class GFG { public static long minSum ( int n , int [ ] num , int [ ] price ) { long [ ] dp = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; long ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = ( long ) Math . Min ( ( long ) dp [ j ] , ( long ) price [ i ] + ( long ) price [ j ] ) ; ans = Math . Min ( ans , ( long ) dp [ i ] + ( long ) price [ j ] ) ; } } } return ans != int . MaxValue ? ans : - 1 ; } public static void Main ( String [ ] args ) { int [ ] num = { 2 , 4 , 6 , 7 , 8 } ; int [ ] price = { 10 , 20 , 100 , 20 , 40 } ; int n = price . Length ; Console . WriteLine ( minSum ( n , num , price ) ) ; } }