Java Map & Priority Queue Solution
\nclass Solution {\n    public int[] findXSum(int[] nums, int k, int x) {\n        int[] result = new int[nums.length - k + 1];\n        for (int i = 0; i < num
```\nclass Solution {\n    public int[] findXSum(int[] nums, int k, int x) {\n        int[] result = new int[nums.length - k + 1];\n        for (int i = 0; i < nums.length - k + 1; i++) {\n            result[i] = getSum(nums, i, i + k - 1, x);\n        }\n        return result;\n    }\n    public int getSum(int[] nums, int start, int end, int x) {\n        Map<Integer, Integer> map = new HashMap<>();\n        for (int i = start; i <= end; i++) {\n            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);\n        }\n        PriorityQueue<Pair<Integer, Integer>> priorityQueue = new PriorityQueue<Pair<Integer, Integer>>((a, b) -> {\n            if (a.getValue() == b.getValue()) {\n                return -Integer.compare(a.getKey(), b.getKey());\n            }\n            return -Integer.compare(a.getValue(), b.getValue());\n        });\n        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {\n            priorityQueue.offer(new Pair(entry.getKey(), entry.getValue()));\n        }\n        int sum = 0;\n        while (x > 0 && !priorityQueue.isEmpty()) {\n            Pair<Integer, Integer> pair = priorityQueue.poll();\n            sum = sum + pair.getKey() * pair.getValue();\n            x--;\n        }\n        return sum;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']