C++ / Java / Python3 solution (Sliding Window, Hash Table, Priority Queue)
IntuitionKeep hash table as sliding window to calculate frequencies.
Use priority queue to get top x elements.Complexity

Time complexity:
O(n×k×log(k))


Space
# Intuition
Keep hash table as sliding window to calculate frequencies.
Use priority queue to get top $x$ elements.

# Complexity
- Time complexity:
$$O(n \times k \times \log(k))$$

- Space complexity:
$$O(k)$$

# Code
```cpp []
#include <functional>

typedef pair<int, int> int_pair;

class Solution {
public:
    vector<int> findXSum(vector<int>& nums, int k, int x) {
        int n = nums.size();

        vector<int> answer(n - k + 1, 0);
        unordered_map<int, int> counter;
        for (int i = 0; i < k; i++) {
            int num = nums[i];
            if (counter.find(num) == counter.end()) {
                counter[num] = 1;
            } else {
                counter[num] += 1;
            }
        }
        answer[0] = get_sum(counter, x);
        
        for (int i = 1; i < n - k + 1; i++) {
            int tail_idx = i - 1;
            int tail = nums[tail_idx];

            int head_idx = k + tail_idx;
            int head = nums[head_idx];

            if (head == tail) {
                answer[i] = answer[i - 1];
                continue;
            }

            int count = counter[tail];
            if (count == 1) {
                counter.erase(tail);
            } else {
                counter[tail] = count - 1;
            }

            if (counter.find(head) == counter.end()) {
                counter[head] = 1;
            } else {
                counter[head] = counter[head] + 1;
            }
            answer[i] = get_sum(counter, x);
        }
        
        return answer;
    }

private:
    function<bool(const int_pair&, const int_pair&)> comparator = [](const int_pair& a, const int_pair& b) {
        int diff = a.second - b.second;
        if (diff != 0) {
            return diff > 0;
        }
        return b.first < a.first;
    };

    int get_sum(unordered_map<int, int>& counter, int x) {
        priority_queue<int_pair, vector<int_pair>, decltype(comparator)> pq(comparator);
        int sum = 0;
        for (auto& kv: counter) {
            pq.push(kv);
            sum += kv.first*kv.second;
            if (pq.size() > x) {
                int_pair top = pq.top();
                pq.pop();
                sum -= top.first*top.second;
            }
        }

        return sum;
    }

};
```
``` java []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        int n = nums.length;

        int[] answer = new int[n - k + 1];

        Map<Integer, Integer> counter = new HashMap<>();
        for (int i = 0; i < k; i++) {
            int num = nums[i];
            counter.put(num, counter.getOrDefault(num, 0) + 1);
        }
        answer[0] = getSum(counter, x);

        for (int i = 1; i < n - k + 1; i++) {
            int tailIdx = i - 1;
            int tail = nums[tailIdx];
            int headIdx = k + tailIdx;
            int head = nums[headIdx];

            if (tail == head) {
                answer[i] = answer[i - 1];
                continue;
            }

            if (counter.get(tail) == 1) {
                counter.remove(tail);
            } else {
                counter.put(tail, counter.get(tail) - 1);
            }
            counter.put(head, counter.getOrDefault(head, 0) + 1);

            answer[i] = getSum(counter, x);
        }

        return answer;
    }

    private int getSum(Map<Integer, Integer> counter, int x) {
        PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(
            new Comparator<Map.Entry<Integer, Integer>>() {
                @Override
                public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b) {
                    if (a.getValue() != b.getValue()) {
                        return a.getValue() - b.getValue();
                    }
                    return a.getKey() - b.getKey();
                }
            });

        int sum = 0;
        for (Map.Entry<Integer, Integer> entry: counter.entrySet()) {
            queue.offer(entry);
            sum += entry.getKey()*entry.getValue();
            if (queue.size() > x) {
                Map.Entry<Integer, Integer> polled = queue.poll();
                sum -= polled.getKey()*polled.getValue();
            }
        }

        return sum;
    }
}
```
``` python []
class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        n = len(nums)
        answer = [0] * (n - k + 1)
        counter = Counter(nums[:k])
        
        answer[0] = self.get_sum(counter, x)

        for i in range(1, n - k + 1):
            tail_idx = i - 1
            tail = nums[tail_idx]
            head_idx = k + tail_idx
            head = nums[head_idx]

            if head == tail:
                answer[i] = answer[i - 1]
                continue

            if counter[tail] == 1:
                del counter[tail]
            else:
                counter[tail] -= 1

            counter[head] += 1
            
            answer[i] = self.get_sum(counter, x)

        return answer

    def get_sum(self, counter: Counter, x: int) -> int:
        pq = [(count, num) for num, count in counter.items()]
        heapq.heapify(pq)
        
        sum_value = sum(num * count for num, count in counter.items())

        while len(pq) > x:
            count, num = heapq.heappop(pq)
            sum_value -= num * count

        return sum_value
```
['Hash Table', 'Sliding Window', 'Heap (Priority Queue)', 'Python', 'C++', 'Java', 'Python3']