Minimize cost required to complete all processes | C ++ program to implement the above approach ; Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool func ( pair < int , int > i1 , pair < int , int > i2 ) { return ( i1 . first - i1 . second < i2 . first - i2 . second ) ; } int minimumCostReqToCompthePrcess ( vector < pair < int , int > > arr ) { sort ( arr . begin ( ) , arr . end ( ) , func ) ; int n = arr . size ( ) ; int minCost = 0 ; int minCostInit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] . second > minCostInit ) { minCost += ( arr [ i ] . second - minCostInit ) ; minCostInit = arr [ i ] . second ; } minCostInit -= arr [ i ] . first ; } return minCost ; } int main ( ) { vector < pair < int , int > > arr = { { 1 , 2 } , { 2 , 4 } , { 4 , 8 } } ; cout << ( minimumCostReqToCompthePrcess ( arr ) ) ; }