Simple Approach
IntuitionApproachComplexity
Time complexity: O(n⋅klogk)

Space complexity: O(n)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n⋅klogk)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> findXSum(vector<int>& nums, int k, int x) {
        int n = nums.size();
        vector<int> answer;
        unordered_map<int, int> freq;
        priority_queue<pair<int, int>> pq;  // max-heap to track frequencies
        
        // Initialize frequency map and max-heap for the first window
        for (int j = 0; j < k; j++) {
            freq[nums[j]]++;
        }
        
        // Fill the priority queue with the first k elements (without re-building in each window)
        for (auto& entry : freq) {
            pq.push({entry.second, entry.first});
        }

        // Calculate the x-sum for the first window
        answer.push_back(calculateXSum(pq, x));

        // Sliding window to process remaining elements
        for (int i = 1; i <= n - k; i++) {
            // Remove the element going out of the window
            freq[nums[i - 1]]--;
            if (freq[nums[i - 1]] == 0) {
                freq.erase(nums[i - 1]);
            }

            // Add the new element coming into the window
            freq[nums[i + k - 1]]++;
            
            // Rebuild the priority queue from the frequency map (to handle element removal)
            pq = priority_queue<pair<int, int>>(); // clear the priority queue
            for (auto& entry : freq) {
                pq.push({entry.second, entry.first});
            }

            // Calculate the x-sum for the current window
            answer.push_back(calculateXSum(pq, x));
        }
        
        return answer;
    }

private:
    int calculateXSum(priority_queue<pair<int, int>>& pq, int x) {
        int sum = 0;
        int count = 0;
        
        // Sum the top x elements from the priority queue
        while (!pq.empty() && count < x) {
            auto top = pq.top();
            pq.pop();
            sum += top.second * top.first; // value * frequency
            count++;
        }

        return sum;
    }
};

```
['C++']