Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | C # program for the above approach ; Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void minSum ( int [ ] arr , int n ) { int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } int s = 0 ; foreach ( int x in ans ) { s = s + x ; } Console . Write ( s + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 } ; int N = arr . Length ; minSum ( arr , N ) ; } }