Easy | Java | Beats 97% | With comments for explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- O(n log n) -->\n\n- Space complexity:\n<!-- O(1) -->\n\n# Code\n```java []\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n        Arrays.sort(happiness);\n        long res=0;\n\n        int n = happiness.length;\n        int count = n; //To determine if we are adding happiness count for the first time\n        int i = 1; //To decrease the happiness count dynamically\n\n        if(k==0) //if k==0 then return 0\n            return res;\n\n        //To run the loop k times \n        while(k>0 && n>=0) { \n            //if happiness becomes less or equal to 0 then stop the loop\n            if(happiness[n-1] == 0)\n                return res;\n            else {\n                //for first addition to res\n                if(count == n) \n                    res += happiness[n-1];\n                //for addition alongwith decreasing the happiness number by 1 each time\n                else if(happiness[n-1] - i > 0){\n                    res += happiness[n-1] - i;\n                    i++;\n                } else {\n                    return res;\n                }\n                n--;\n            }\n            k--; //decreasing k by 1 \n        }\n        return res;\n    }\n}\n```
['Java']