Sliding window for dictionary, 97% speed
Code
# Code
```python3 []
class Solution:

    def sum_x(self, window: dict, freq: int) -> int:
        lst = [(window[key], key) for key in window]
        lst.sort(reverse=True)
        # Adjust the frequency to the minimum of the length of lst and freq
        freq = min(freq, len(lst))
        return sum(lst[i][0] * lst[i][1] for i in range(freq))

    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        window_dict = dict()

        # Initialize the sliding window dictionary with the first 'k' elements
        for i in range(k):
            if nums[i] in window_dict:
                window_dict[nums[i]] += 1
            else:
                window_dict[nums[i]] = 1

        # Initialize the answer list with zeroes and set the first value
        ans = [0] * (len(nums) - k + 1)
        ans[0] = self.sum_x(window_dict, x)

        # Slide the window across the array
        for i in range(len(ans) - 1):
            # Decrement the frequency of the outgoing element
            window_dict[nums[i]] -= 1
            if window_dict[nums[i]] == 0:
                window_dict.pop(nums[i])

            # Increment the frequency of the incoming element
            right = i + k
            if nums[right] in window_dict:
                window_dict[nums[right]] += 1
            else:
                window_dict[nums[right]] = 1

            # Compute the sum for the current window and update the answer list
            ans[i + 1] = self.sum_x(window_dict, x)

        return ans
```
['Python3']