Solution using dictionary and sorting
Code


# Code
```python3 []
class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        ans = []
        left = 0
        for i in range(len(nums)+1):
            dic = {}
            if(i>=k):
                sub = nums[left:i]
                valcount = 0
                single = 0
                sol = []
                singlelements = []
                for j in sub:
                    dic[j] = dic.get(j,0)+1
                for val,count in dic.items():
                    if(count >=x ):
                        valcount+=1
                        sol.append((val*count,count))
                    if(count<x):
                        single+=1
                        singlelements.append((val*count,count))
                sol.sort(reverse = True,key = lambda x:(x[1],x[0]))
                singlelements.sort(reverse = True , key = lambda x:(x[1],x[0]))
                diff = x - len(singlelements)
                if(len(sol)>=x):
                    ans.append(sum(tup[0] for tup in sol[:x]))
                elif(len(sol) < x and len(singlelements) >= diff):
                    ans.append(sum(tupp[0] for tupp in sol[:x])+ sum(tuple[0] for tuple in singlelements[:x-len(sol)]))
                else:
                    ans.append(sum(sub))
                left+=1
        return ans              




                
                
                
                




        
        
```
['Python3']