Easy Video Solution 🔥 || Greedy || Simple Sorting + Count ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nSort the array and dry run few scenarios\n\n\nEasy Video Explanation\n\nhttps://youtu.b
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort the array and dry run few scenarios\n\n\n***Easy Video Explanation***\n\nhttps://youtu.be/8F2paPNQdpo\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N*log(N))\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n    \n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& v, int k) {\n        sort(v.begin(),v.end());\n        long long ans = 0;\n        int n=v.size()-1;\n        int c=0;\n        while(n>=0 && k>0) {\n            if(v[n]-c>=0)\n                ans=ans+v[n]-c;\n            else\n                break;\n            c++;\n            n--;\n            k--;\n        }\n        return ans;\n    }\n};\n```\n```python []\nclass Solution:\n    def maximumHappinessSum(self, v, k):\n        v.sort()\n        ans = 0\n        n = len(v) - 1\n        c = 0\n        while n >= 0 and k > 0:\n            if v[n] - c >= 0:\n                ans += v[n] - c\n            else:\n                break\n            c += 1\n            n -= 1\n            k -= 1\n        return ans\n\n```\n```Java []\nimport java.util.Arrays;\n\npublic class Solution {\n    public long maximumHappinessSum(int[] v, int k) {\n        Arrays.sort(v);\n        long ans = 0;\n        int n = v.length - 1;\n        int c = 0;\n        while (n >= 0 && k > 0) {\n            if (v[n] - c >= 0) {\n                ans += v[n] - c;\n            } else {\n                break;\n            }\n            c++;\n            n--;\n            k--;\n        }\n        return ans;\n    }\n}\n\n\n```\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you* \uD83D\uDE00
['Sorting', 'Counting', 'C++', 'Java', 'Python3']