Python3 || 5 lines, nlargest, mapping  || T/S: 81% / 58%
Here\'s the intuition:\n- This problem is really a series of problems, each on a k-length subarray of nums. \n- Thus, we can determine a function to find the su
Here\'s the intuition:\n- This problem is really a series of problems, each on a *k*-length subarray of `nums`. \n- Thus, we can determine a function to find the sum for each such a subarray and then map it to each subarray to determine the answer.\n```python3 []\nclass Solution:\n    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:\n\n        def do_sum (idx: int)-> int:\n\n            ctr = Counter(nums[idx:idx + k])\n            most_freq = nlargest(x, ctr, key = lambda y: (ctr[y], y))\n\n            return sum(map(lambda y: y * ctr[y], most_freq))\n\n\n        return map(do_sum,range(len(nums)+1 - k))\n```\n```cpp []\nclass Solution {\npublic:\n    vector<int> findXSum(vector<int>& nums, int k, int x) {\n        vector<int> result;\n\n        auto do_sum = [&](int idx) -> int {\n            unordered_map<int, int> ctr;\n\n            for (int i = idx; i < idx + k; ++i) {\n                ctr[nums[i]]++;}\n\n            priority_queue<pair<int, int>> pq;\n\n            for (auto& p : ctr) {\n                pq.push({p.second, p.first}); }\n\n            int sum = 0;\n            for (int i = 0; i < x && !pq.empty(); ++i) {\n                sum += pq.top().second * pq.top().first;\n                pq.pop();}\n\n            return sum;};\n\n        for (int i = 0; i <= nums.size() - k; ++i) {\n            result.push_back(do_sum(i));}\n\n        return result;}\n};\n```\n[https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/submissions/1421175528/](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/submissions/1421175528/)\n\n\n\nI could be wrong, but I think that time complexity is *O*(*NKX*) and space complexity is *O*(*K*), in which *N* ~ `len(nums)`, *K* ~ `k`, and *X* ~ `x`.\n
['C++', 'Python3']