Two Java Solutions with PriorityQueue and TreeSet
IntuitionApproachComplexity
Time complexity:

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

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

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

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

# Code
```java []
class Solution {
    public int[] findXSum1(int[] nums, int k, int x) {
        int len = nums.length - k + 1;
        if (len < 1) {
            len = 1;
        }
        int[] result = new int[len];

        HashMap<Integer, Integer> map = new HashMap<>();
        // init map
        for ( int i = 0; i < k && i< nums.length; i++) {
            if (map.containsKey(nums[i])) {
                map.put(nums[i], map.get(nums[i]) + 1);
            } else {
                map.put(nums[i], 1);
            }
        }

        for (int i = 0; i < len; i++) {
            // count result[i]
            result[i] = sum1(map, x);
            //remove nums[i]
            map.put(nums[i], map.get(nums[i]) - 1);
            if ((i + k) < nums.length) {
                if (map.containsKey(nums[i + k])) {
                    map.put(nums[i + k], map.get(nums[i + k]) + 1);
                } else {
                    map.put(nums[i + k], 1);
                }
            }
        }

        return result;
    }

    int sum1(HashMap<Integer, Integer> map, int x) {
        PriorityQueue<Element> queue = new PriorityQueue<>(new MyComparator());
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            queue.add(new Element(entry.getValue(), entry.getKey()));
        }

        int sum = 0;
        for (int i = 0; i < x  && !queue.isEmpty(); i++) {
            sum += queue.poll().sum();
        }
        return sum;
    }

    public int[] findXSum(int[] nums, int k, int x) {
        int len = nums.length - k + 1;
        if (len < 1) {
            len = 1;
        }
        int[] result = new int[len];

        HashMap<Integer, Element> map = new HashMap<>();
        TreeSet<Element> treeSet = new TreeSet<>(new MyComparator());

        // init map
        for ( int i = 0; i < k && i< nums.length; i++) {
            addOrIncreaseElement(treeSet, map, nums[i]);
        }

        for (int j = 0; j < len; j++) {
            // count result[j]
            result[j] = sum2(treeSet, x);
            // sliding window
            // remove first
            decreaseCount(treeSet, map.get(nums[j]));
            // add new
            if ((j + k) < nums.length) {
                addOrIncreaseElement(treeSet, map, nums[j + k]);
            }
        }

        return result;
    }

    private void addOrIncreaseElement(TreeSet<Element> treeSet, HashMap<Integer, Element> map, int num) {
        Element e = map.get(num);
        if (e != null) {
            increaseCount(treeSet, e);
        } else {
            e = new Element(1, num);
            map.put(num, e);
            treeSet.add(e);
        }
    }

    int sum2(TreeSet<Element> treeSet, int x) {
        Iterator<Element> it = treeSet.iterator();
        int sum = 0;
        for (int i = 0; it.hasNext() && i < x; i++) {
            sum += it.next().sum();
        }
        return sum;
    }

    private void increaseCount(TreeSet<Element> treeSet, Element element) {
        treeSet.remove(element);
        // must remove first, otherwise treeset cannot locate the element
        element.count++;
        treeSet.add(element);
    }

    private void decreaseCount(TreeSet<Element> treeSet, Element element) {
        treeSet.remove(element);
        element.count--;
        if (element.count > 0) {
          treeSet.add(element);
        }
    }

    class Element {
        int count;
        int val;

        Element(int count, int val) {
            this.count = count;
            this.val = val;
        }

        int sum() {
            return count * val;
        }
    }

    // descending
    class MyComparator implements Comparator<Element> {
        @Override
        public int compare(Element o1, Element o2) {
            if (o1.count != o2.count) {
                return o2.count - o1.count;
            } else {
                return o2.val - o1.val;
            }
        }
    }
}
```
['Java']