Maximize sum of array elements removed by performing the given operations | C # program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { List < List < int > > A = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . Add ( new List < int > ( ) ) ; A [ i ] . Add ( arr [ i ] ) ; A [ i ] . Add ( min [ i ] ) ; } A = A . OrderBy ( lst => lst [ 0 ] ) . ToList ( ) ; A . Reverse ( ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . Max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) ; K += k ; } } static public void Main ( ) { int [ ] arr = { 3 , 5 , 2 , 1 } ; int [ ] min = { 3 , 2 , 1 , 3 } ; int N = arr . Length ; int K = 3 ; S = 0 ; findMaxSum ( arr , N , min , K ) ; Console . WriteLine ( S ) ; } }