Maximizing Happiness Sum: Selecting Children in Turns
Intuition\nTo maximize the sum of happiness values while selecting children, we should prioritize selecting children with higher initial happiness values.\n Des
# Intuition\nTo maximize the sum of happiness values while selecting children, we should prioritize selecting children with higher initial happiness values.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Sort the array of happiness values in non-decreasing order.\n2. Iterate through the sorted array from the end and select the children with the highest happiness values.\n3. Decrement the happiness values of unselected children by the number of turns taken.\n4. Repeat this process for k turns.\n5. Sum up the happiness values of the selected children and return the total sum.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n log n) due to sorting, where n is the number of children.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) since we only use a constant amount of extra space for variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& happiness, int k) {\n        sort(happiness.begin(), happiness.end());\n        int n=happiness.size();\n        long long ans=0;\n        for(int i=0; i<k; i++){\n            if(happiness[n-i-1]-i>0){\n                ans+=happiness[n-i-1]-i;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']