Sorting + Greedy solution
Approach\n Describe your approach to solving the problem. \nTo achieve the maximum sum from $K$ elements of the array, we sort it in descending order and select
# Approach\n<!-- Describe your approach to solving the problem. -->\nTo achieve the maximum sum from $K$ elements of the array, we sort it in descending order and select the first $K$ members.\n\nWe subtract the value of the step from the value at that position in the array at each step. If the current value becomes less than zero, we terminate the loop.\n\n# Complexity\n- Time complexity: $O(N * log N)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$ additional space\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    static long long maximumHappinessSum(std::vector<int> &happiness, int k) {\n        std::sort(happiness.begin(), happiness.end(), greater<int>());\n        long long result = 0;\n        for (size_t step = 0; step < k; step++) {\n            int value = happiness[step] - static_cast<int>(step);\n            if (value < 0) break;\n            result += value;\n        }\n        return result;\n    }\n};\n\n```\n\nMost of my solutions are in [C++](https://github.com/drgavrikov/leetcode-cpp) and [Kotlin](https://github.com/drgavrikov/leetcode-jvm) on my [Github](https://github.com/drgavrikov).
['Sorting', 'C++']