Beats 100% | Java,  C++, Python, Javascript| Less Code ✅✅
Please Upvote \u2705\u2705\nans: A long variable initialized to 0 to store the maximum happiness sum.\nmin: An integer variable initialized to 0 to keep track o
# Please Upvote \u2705\u2705\nans: A long variable initialized to 0 to store the maximum happiness sum.\nmin: An integer variable initialized to 0 to keep track of the minimum happiness given so far.\n\n- The happiness array is sorted in descending order using Arrays.sort(happiness). This ensures that people with higher happiness levels get prioritized.\n\n- The code iterates through the happiness array in reverse order (from highest happiness to lowest) using a for loop starting from i = happiness.length - 1.\n- The loop continues as long as i is greater than or equal to 0 and k (remaining people to be happy) is greater than 0.\n- Inside the loop, for each person (happiness[i]), the difference between their happiness and the min happiness given so far is calculated. This represents the additional happiness gained by giving happiness to this person.\n- If the happiness gain (happiness[i] - min) is positive (meaning the person gets happier), it\'s added to the ans (total happiness sum).\nThe min happiness is then incremented by 1 to account for the happiness given to the current person.\n- After iterating through all relevant elements, the function returns the ans (maximum happiness sum achievable).\n\n# Please Upvote\u2705\u2705\n# Code\n``` java []\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n        long ans = 0;\n        int min = 0;\n        \n        Arrays.sort(happiness);\n        \n        for(int i=happiness.length-1;i>=0 && k>0;i--){\n           \n            k--;\n            if(min<happiness[i])\n            ans+=happiness[i]-min;\n            min++;\n        }\n        return ans;\n    }\n}\n```\n```javascript []\n\nvar maximumHappinessSum = function(happiness, k) {\n    happiness.sort((a, b) => b - a);\n\n  let ans = 0;\n  let minHappiness = 0;\n\n  for (let i = 0; i < happiness.length && k > 0; i++) {\n    if (minHappiness < happiness[i]) {\n      ans += happiness[i] - minHappiness;\n    }\n    minHappiness++;\n    k--;\n  }\n\n  return ans;\n};\n```\n```python []\nclass Solution:\n    def maximumHappinessSum(self, happiness: List[int], k: int) -> int:\n        ans = 0\n        min_happiness = 0\n\n       \n        happiness.sort(reverse=True)\n\n        for i in range(len(happiness)):\n            if i >= k:\n                break\n\n          \n            happiness_gain = happiness[i] - min_happiness\n\n            \n            ans += happiness_gain\n\n            min_happiness += 1\n\n        return ans\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& happiness, int k) {\n        long long ans = 0;\n  int minHappiness = 0;\n\n  \n  sort(happiness.begin(), happiness.end(), std::greater<int>());\n\n  for (int i = 0; i < k; i++) {\n    if (minHappiness < happiness[i]) {\n      ans += happiness[i] - minHappiness;\n    }\n    minHappiness++;\n  }\n\n  return ans;\n    }\n};\n```\n# Just\n![smile.png](https://assets.leetcode.com/users/images/9f237316-25f2-4190-bc0c-2900f05166db_1710045853.4063056.png)\n\n
['Array', 'C', 'Sorting', 'C++', 'Java', 'Go', 'Python3', 'Kotlin', 'JavaScript', 'C#']