classSolution{public:inttwoCitySchedCost(vector<vector<int>>&costs){constintn=costs.size()/2;intans=0;// How much money can we save if we fly a person to A instead of B?// To save money, we should// 1) fly the person with the max saving to A// 2) fly the person with the min saving to Bsort(begin(costs),end(costs),[](constauto&a,constauto&b){// Sort in descending order by the money saved// If we fly a person to A instead of Breturna[1]-a[0]>b[1]-b[0];});for(inti=0;i<n;++i)ans+=costs[i][0]+costs[i+n][1];returnans;}};
classSolution{publicinttwoCitySchedCost(int[][]costs){finalintn=costs.length/2;intans=0;// How much money can we save if we fly a person to A instead of B?// To save money, we should// 1) fly the person with the max saving to A// 2) fly the person with the min saving to B// Sort in descending order by the money saved if we fly a person to A instead of BArrays.sort(costs,(a,b)->(b[1]-b[0])-(a[1]-a[0]));for(inti=0;i<n;++i)ans+=costs[i][0]+costs[i+n][1];returnans;}}
classSolution:deftwoCitySchedCost(self,costs:List[List[int]])->int:n=len(costs)//2# How much money can we save if we fly a person to A instead of B?# To save money, we should# 1) fly the person with the max saving to A# 2) fly the person with the min saving to B# Sort in descending order by the money saved if we fly a person to Acosts.sort(key=lambdax:x[0]-x[1])returnsum(costs[i][0]+costs[i+n][1]foriinrange(n))