C++ solution
IntuitionApproachComplexity
Time complexity: O(N)

Space complexity: O(N)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution 
{

    public:
    vector<int> findXSum(vector<int>& nums, int k, int x) 
    {
        int n = nums.size() - k + 1;
        vector<int> answer (n);
        for(int i = 0; i < n; i++)
        {
            vector<int> sub_nums = {nums.begin() + i,nums.begin() + i + k};
            answer[i] = x_sum(sub_nums,x);
        }
        return answer;
    }

    private:
    int x_sum(vector<int>& nums, int x)
    {
        map<int,int> freq;
        for(int num: nums)
        {
            freq[num]++;
        }
        vector<pair<int,int>> freq_vec (freq.size());
        int i = 0;
        for(auto pair: freq)
        {
            freq_vec[i].first = pair.first;
            freq_vec[i].second = pair.second;
            i++;
        }
        sort(freq_vec.begin(),freq_vec.end(),cmp);
        int sum = 0;
        vector<pair<int,int>>::iterator it = freq_vec.begin();
        while(x > 0 && it != freq_vec.end())
        {
            sum += it->first * it->second;
            x--;
            it++;
        }
        return sum;
    }
    
    bool static cmp(const pair<int,int>& a, const pair<int,int>& b)
    {
        return ((a.second == b.second)? a.first > b.first : a.second > b.second);
    }

};
```
['C++']