Javascript
Code
# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {number} k
 * @param {number} x
 * @return {number[]}
 */
var findXSum = function(nums, k, x) {
    const result = [];

    for (let i = 0; i <= nums.length - k; i++) {
        const subarray = nums.slice(i, i + k);
        const freqMap = new Map();

        // Count occurrences
        for (const num of subarray) {
            freqMap.set(num, (freqMap.get(num) || 0) + 1);
        }

        // Convert map to array and sort by frequency and value
        const sorted = Array.from(freqMap.entries()).sort((a, b) => {
            if (b[1] === a[1]) return b[0] - a[0]; // Sort by value if frequency is equal
            return b[1] - a[1]; // Sort by frequency
        });

        // Take top x most frequent elements
        const topX = sorted.slice(0, x);

        // Calculate sum
        const sum = topX.reduce((acc, [num, freq]) => acc + num * freq, 0);
        result.push(sum);
    }

    return result;     
};
```
['JavaScript']