PriorityQueue solution using TreeMap.
IntuitionUse a Map and MaxHeap to allow retrieval of most frequent elements from the given array, and compute sum from the array[i...i+k-1]ApproachFor each iter
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use a Map and MaxHeap to allow retrieval of most frequent elements from the given array, and compute sum from the array[i...i+k-1]
# Approach
<!-- Describe your approach to solving the problem. -->
For each iteration create a map of current set of elements, store the map entry in PriorityQueue and compute sum.

Logic for compute Sum:
compute sum of elements in the pq, multiplying each element with the frequency of the element in the map. 

# 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 {
    PriorityQueue<Map.Entry<Integer,Integer>> pq;
    Map<Integer,Integer> map;
    public int[] findXSum(int[] nums, int k, int x) {
        map = new TreeMap<>();
        int n=nums.length;
        pq=new PriorityQueue<>((a,b)->(a.getValue()==b.getValue()?b.getKey()-a.getKey():b.getValue()-a.getValue()));
        
        int[] res=new int[n-k+1];
        for(int i=0;i<n-k+1;i++){
            res[i]=xSum(nums,i,k,x);
        }
        return res;
    }

    int xSum(int[] nums, int i, int k, int x){
        // if (map.size() < x) return cdf(nums);
        map.clear();
        pq.clear();
        for(int loop=i;loop<i+k;loop++){
             map.put(nums[loop],map.getOrDefault(nums[loop],0)+1);
        }
        // System.out.println(map);
        for(Map.Entry<Integer,Integer> entry:map.entrySet())
            pq.add(entry);
        // System.out.println("priority q: " + pq);

        int[] arr=new int[x];
        int a=0,sum=0;
        if(pq.size()<x) return cdf(pq);
        while(a<x){
            int topElement = pq.poll().getKey();
            sum+=topElement * map.get(topElement);
            a++;
        }
      
       return sum;
    }

    int cdf(PriorityQueue<Map.Entry<Integer,Integer>> pq){
        int sum=0;
        for(Map.Entry<Integer,Integer> e:pq){
            sum+=e.getKey()*e.getValue();
        }
        return sum;
    }
}
```
['Java']