Maximize sum of array elements removed by performing the given operations | Java 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
import java . util . * ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { ArrayList < int [ ] > A = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . add ( new int [ ] { arr [ i ] , min [ i ] } ) ; } Collections . sort ( A , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . max ( A . get ( i ) [ 0 ] - K , A . get ( i ) [ 1 ] ) ; K += k ; } } public static void main ( String [ ] args ) { 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 ) ; System . out . println ( S ) ; } }