Greedy Approach with explanation
Intuition\n- We can greedily pick child with maximum happiness\n- After n pass happiness of childrens decreases by n so if we are picking children at xth time h
# Intuition\n- We can greedily pick child with maximum happiness\n- After `n` pass happiness of childrens decreases by `n` so if we are picking children at xth time his net happiness will be $$h[x]-x$$\n- We can sort the array and can pick all children who will have positive net happiness \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Sort array in descending order\n- Start picking children from 0th index and add its net happiness in result\n- break the iteration if net happiness if false or we have reached our quota of n students\n# Complexity\n- Time complexity: O(nlogn)\n    - O(nlogn) for sorting array\n    - O(n) for linear traversal and getting result\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: Constant Space no extra space needed\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```c++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& h, int k) {\n        sort(h.begin(), h.end(), greater<int>());\n        int i=0;\n        long long res = 0l;\n        while(i<h.size() && i<k) {\n            if(h[i]<=i)break;\n            res = res + h[i] - i;\n            i++;\n        }\n        return res;\n    }\n};\n```\n```go []\nfunc maximumHappinessSum(h []int, k int) int64 {\n    sort.Slice(h, func(i, j int) bool {\n        return h[i] > h[j]\n    })\n\n    var res int64 = 0\n    for i := 0; i < len(h) && i < k; i++ {\n        if h[i] <= i {\n            break\n        }\n        res += int64(h[i] - i)\n    }\n    return res \n}\n```\n```python []\nclass Solution(object):\n    def maximumHappinessSum(self, h, k):\n        h.sort(reverse=True)\n        res = 0\n        for i in range(min(len(h), k)):\n            if h[i] <= i:\n                break\n            res += h[i] - i\n        return res\n        \n```\n
['Greedy', 'Python', 'C++', 'Go']