Sliding Window + Map Heap (Priority Queue) 
ApproachStep-by-Step Explanation:
Understanding the Input:
You have an array nums that contains integers. For example, nums = [1, 1, 2, 2, 3, 4].
You also have
# Approach
  Step-by-Step Explanation:

1) Understanding the Input:

You have an array nums that contains integers. For example, nums = [1, 1, 2, 2, 3, 4].
You also have two integers: k, which specifies the length of the subarrays you want to consider (e.g., k = 3), and x, which specifies how many of the most frequent elements you want to include in your sum (e.g., x = 2).

2) Setting Up the Result Array:

You need to create an array res to store the x-sum for each subarray of length k. The size of this array will be n - k + 1, where n is the length of nums. This is because there are n - k + 1 possible starting points for subarrays of length k.
For example, if n = 6 and k = 3, then res will have a size of 6 - 3 + 1 = 4.

3) Iterating Through the Array:

You will use a loop to go through the array nums. The loop will run from 0 to n - k, which allows you to extract each subarray of length k.
For each position i, you extract the subarray starting at index i and ending at index i + k - 1. This can be done using Arrays.copyOfRange(nums, i, i + k).
For example, if i = 0, the subarray is [1, 1, 2]. If i = 1, the subarray is [1, 2, 2], and so on.

4) Counting Frequencies:

For each subarray, you need to count how many times each number appears. This is done using a HashMap, where the key is the number and the value is its count.
For example, for the subarray [1, 1, 2], the counts would be {1: 2, 2: 1} because 1 appears twice and 2 appears once.

5) Finding the Top x Frequent Elements:

To find the top x most frequent elements, you can use a priority queue (or a max-heap). This data structure allows you to efficiently retrieve the elements with the highest frequency.
You add all the entries from the frequency map to the priority queue. The priority queue is set up to sort first by frequency (in descending order) and then by value (also in descending order) if frequencies are the same.
For example, if you have counts {1: 2, 2: 1}, the priority queue will prioritize 1 (because it has the highest frequency) over 2.

6) Calculating the x-sum:

Initialize a variable sum to zero. This will hold the x-sum for the current subarray.
Poll (remove) the top x elements from the priority queue. For each of these elements, calculate the contribution to the sum by multiplying the number by its frequency.
For example, if the top two elements are 1 (2 times) and 2 (1 time), the x-sum would be calculated as 1 * 2 + 2 * 1 = 4.

7) Storing the Result:

After calculating the x-sum for the current subarray, store this value in the res array at the index corresponding to the starting position of the subarray.
For example, if you calculated the x-sum for the subarray starting at index 0, you would store it in res[0].

8) Returning the Result:

After processing all possible subarrays, return the res array, which now contains the x-sums for each subarray of length k.
For example, if you processed all subarrays and calculated their x-sums, the final result might look like res = [4, 5, 7, 6]

# Complexity
- Time complexity:
  O(N∗K∗Log(K))

- Space complexity:
  O(N)

# Code
```java []
class Solution {
    public int calSum(int []arr,int x){
        
        Map<Integer, Integer> map = new HashMap<>();

        for (int num : arr) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }

        // Create a priority queue to sort by frequency and then by value
        PriorityQueue<Map.Entry<Integer, Integer>>pq = new PriorityQueue<>((a,b) -> {
            if(a.getValue() == b.getValue()) return b.getKey()-a.getKey(); // Higher Value First
            return b.getValue()-a.getValue(); // Higher Frequency First
        });

        pq.addAll(map.entrySet());

        int cnt = 1, sum = 0;
        
        // Get the top x elements
        while (!pq.isEmpty() && cnt<=x) {
            Map.Entry<Integer, Integer> entry = pq.poll();
            // Add the occurrences of the element to the sum
            sum+= entry.getKey()*entry.getValue();
            cnt++;
        }

        return sum;
    }

    public int[] findXSum(int[] nums, int k, int x) {
        
        int n = nums.length;
        
        int[] res = new int[n - k + 1];
        
        for (int i = 0; i <= n - k; i++) {
            
            // Get the current subarray of length k
            int[] temp = Arrays.copyOfRange(nums, i, i + k);
            
            // Calculate the x-sum for the current subarray
            res[i] = calSum(temp, x);
        }
        return res;
    }
}
```
['Array', 'Sliding Window', 'Heap (Priority Queue)', 'Java']