sliding window with sort map
Code
# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {number} k
 * @param {number} x
 * @return {number[]}
 */
var findXSum = function (nums, k, x) {
    let begin = 0
    let windowState = new Map()
    let result = []

    for (let end = 0; end < nums.length; end++) {
        windowState.set(nums[end], (windowState.get(nums[end]) || 0) + 1)

        if (end - begin + 1 === k) {
            result.push(xSum(windowState, x))
            windowState.set(nums[begin], windowState.get(nums[begin])  - 1)
            if (windowState.get(nums[begin]) === 0) windowState.delete(nums[begin])
            begin++
        }
    }

    return result
};

function xSum(map, x) {
    let entries = Array.from(map.entries());
    entries.sort((a, b) => {
        if (b[1] === a[1]) return b[0] - a[0]; 
        return b[1] - a[1]; 
    });

    let sum = 0;
    for (let i = 0; i < Math.min(x, entries.length); i++) {
        sum += entries[i][0] * entries[i][1];
    }
    return sum;
}

```
['Hash Table', 'Sliding Window', 'JavaScript']