Kotlin Sugar Style + Decomposition
How it work:
Create Res Array
Iterate over array
For each index calculate x - sum
Create map and iterate and calculate count of occurances
Create Pq and fill
It
**How it work:**

1. Create Res Array
2. Iterate over array
3. For each index calculate x - sum
4. Create map and iterate and calculate count of occurances
5. Create Pq and fill 
6. Iterate take x and calc sum

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

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

# Code
```kotlin []
class Solution {
    fun findXSum(nums: IntArray, k: Int, x: Int): IntArray {
        if (nums.hasSingle()) return intArrayOf(nums[0])
        val n = nums.size
        val res = IntArray(n - k + 1)

        for (i in 0 until res.size) {
            res[i] = nums.calculateXSum(i, k + i - 1, k, x)
        }

        return res
    }

    private fun IntArray.hasSingle(): Boolean = when {
        this.size == 1 -> true
        else -> false
    }


    private fun IntArray.calculateXSum(startIndex: Int, endIndex: Int, k: Int, x: Int): Int {
        when {
            startIndex > endIndex -> throw IndexOutOfBoundsException("$startIndex")
            endIndex > this.size -> throw IndexOutOfBoundsException("$endIndex")
        }
        val counts = mutableMapOf<Int, Int>()
        for (i in startIndex..endIndex) counts[this[i]] = counts.getOrDefault(this[i], 0) + 1

        val pq = PriorityQueue<Pair<Int, Int>> {a: Pair<Int, Int>, b: Pair<Int, Int> -> 
            if (a.second != b.second) b.second - a.second
            else b.first - a.first
        }

        for ((num, count) in counts) pq.offer(Pair(num, count))

        var x = x
        var sum = 0
        while (pq.isNotEmpty() && x != 0) {
            val (num, count) = pq.poll()
            sum += (num * count)
            x--
        }

        return sum
    }

}
```
['Hash Table', 'Heap (Priority Queue)', 'Kotlin']