The most beautiful and easy to understand solution to the most difficult "easy" problem.
ApproachAt first, we initialize an array answer of length n - k + 1 to store the x-sums of each subarray.
Then while iterating through each subarray we define t
# Approach
At first, we initialize an array answer of length n - k + 1 to store the x-sums of each subarray.
Then while iterating through each subarray we define three variables: 
- sum - for calculating the sum of elements,  
- set - for tracking distinct elements 
- freqMap  - for tracking frequencies.

During each iteration of the array using a sliding window of size k, for each window, we calculate:
- the sum of elements,
- the frequency for each element in the subarray by updating the freqMap,
- the unique elements by adding them into the set.

**If there are fewer than x distinct elements, we simply sum all elements of the subarray and store it in answer[i].**

**If there are x or more distinct elements, we use the priority queue to sort the elements by frequency and value.**

**We poll the top x elements and compute their contribution to the x-sum, and store the result in answer[i].**

After processing all subarrays, we return the answer array containing the x-sums for all the subarrays of length k.

# Complexity
- Time complexity:
O(n * k log k).

- Space complexity:
O(n + k)

# Code
```java []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        int n = nums.length;
        int[] answer = new int[n - k + 1];

        for (int i = 0; i < answer.length; i++) {
            int sum = 0;
            Set<Integer> set = new HashSet<>();
            Map<Integer, Integer> freqMap = new HashMap<>();

            for (int j = i; j < i + k; j++) {
                sum += nums[j];
                set.add(nums[j]);
                freqMap.put(nums[j], freqMap.getOrDefault(nums[j], 0) + 1);
            }

            if (set.size() < x) {
                answer[i] = sum;
            } else {
                PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a, b) -> {
                    if (freqMap.get(a) == freqMap.get(b)) {
                        return b - a;
                    }
                    return freqMap.get(b) - freqMap.get(a);
                });

                for (int element : set) {
                    priorityQueue.offer(element);
                }

                int topCount = x;
                while (topCount > 0) {
                    int topElement = priorityQueue.poll();
                    answer[i] += topElement * freqMap.get(topElement);
                    topCount--;
                }
            }
        }
        return answer;
    }
}
```
['Array', 'Hash Table', 'Sliding Window', 'Heap (Priority Queue)', 'Java']