🔥Today's Contest Sol | ✅️Beats 100% ✅️| Easy C++ | Full Explanation🔥
Intuition\n\n Describe your first thoughts on how to solve this problem. \nWe prioritize selecting children with higher happiness values initially to maximize t
# Intuition\n![contest happiness.png](https://assets.leetcode.com/users/images/4d9c4c35-7616-4f52-9e36-a02657f6a17c_1710045051.8879497.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe prioritize selecting children with higher happiness values initially to maximize the sum of happiness. Then we decrement the happiness value of each selected child by its index, ensuring it remains positive, to maximize the overall happiness sum.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the happiness array in non-increasing order.\n2. Initialize the sum of happiness, ans, to 0.\n3. Iterate k times:\n   a. Decrement the happiness value of the current child by its index (starting from 0) or set it to 0 if negative.\n   b. Add the updated happiness value to ans.\n4. Return ans.\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n# Please UPVOTE if you like it : )\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& h, int k) {\n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        \n        sort(h.begin(),h.end(),greater<int>());\n        \n        int i=0;\n        long long ans=0;\n        while(k--){\n            h[i]=max(h[i]-i,0);\n            ans+=h[i];\n            i++;\n        }\n        return ans;\n    }\n};\n```\n![THIS IS BILL.png](https://assets.leetcode.com/users/images/30c74e7a-b68a-483e-83fe-91711c01f87e_1710045188.0636377.png)\n
['C++']