O(n) ✔|| EASY APPROACH ✔|| PLEASE UPVOTE😊||
\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nHere\'s an explanation of the al
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere\'s an explanation of the algorithm\'s intuition:\n\n1. **Sort the happiness values**: The algorithm begins by sorting the given happiness values in non-increasing order. This step ensures that we start with the highest happiness values, which allows us to maximize the total happiness sum.\n\n2. **Iterate over the first k elements**: We iterate over the first k elements of the sorted happiness values. These are the elements we are allowed to reduce to zero.\n\n3. **Accumulate total happiness**: For each element in the iteration, we accumulate its happiness value to the total happiness sum (`count`). If an element has a non-positive value (h[i] <= 0), it means it won\'t contribute to the total happiness, so we return the current total happiness.\n\n4. **Reduce subsequent elements**: For each element in the iteration, we reduce the next element\'s happiness value by the index of the current element plus one (i+1). This step ensures that we decrease the next element\'s value by the number of elements already processed, as per the problem\'s constraint.\n\n5. **Return the total happiness sum**: After iterating over the first k elements and adjusting subsequent elements\' values, we return the total happiness sum accumulated during the process.\n\nOverall, the algorithm prioritizes maximizing the total happiness sum by starting with the highest happiness values and reducing subsequent values strategically based on the number of already processed elements. This ensures that we meet the constraint of reducing only the first k elements and maximize the total happiness sum.\n\n# Complexity\n- Time complexity:O(n)\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\n# Code\n```\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& h, int k) {\n        sort(h.rbegin(), h.rend());\n        long long count = 0;\n        int i = 0;\n        while (i < k) {\n            if (h[i] <= 0) return count;\n            count += h[i];\n            if (i + 1 < h.size()) {\n                h[i + 1] -= (i + 1);\n            }\n            i++;\n        }\n        return count;\n    }\n};\n\n```\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/2374fef3-acf7-4c38-80a9-96845b9b118c_1715228353.3075688.jpeg)\n
['C++']