• + 0 comments

    Here is my Solution in Cpp | Greedy Approach - I sorted the vector first according to the importance.

    int luckBalance(int k, vector<vector<int>> contests) {
        sort(contests.begin(), contests.end(), [](const vector<int> a, const vector<int> b){
            if(a[1]==b[1])
                    return a[0] > b[0];
            return a[1] < b[1];
        });
        
        int ans=0;
        for(auto arr : contests){
            int luck = arr[0];
            int imp= arr[1];
            
            if(imp == 0){
                ans+=luck;
            }
            if(imp == 1){
                if(k  > 0){
                    ans+=luck;
                    k--;
                }
                else{
                    ans -= luck;
                }
                
            }
        }
        
    //     for(auto it  : contests)
    //         for(auto i : it)
    //                 cout<<i<<" ";
    //         cout<<endl;
        
        return ans;
    }