pure C
Approach\n1. Sliding window\n- Use two pointer, left and right to mark the boundaries of the sliding window.\n- Move right to right until its size reaches k\n- 
# Approach\n1. Sliding window\n- Use two pointer, left and right to mark the boundaries of the sliding window.\n- Move right to right until its size reaches ```k```\n- Once the size reaches, calculate the sum of elements in the window and then move left.\n2. Calculate the sum of top ```x``` elements.\n- Traverse all elements in the window and use ```nodeEntry``` structure to store the count of each number.\n- Use ```qsort()``` which sort by their frequency in descending order first, and sort by the value.\n- Finally, calculate the sum of the ```x```\n\n# Complexity\n- Time complexity: \n$$O((n - k + 1) * (k + k log k))$$ \n= $$O(n * klog k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```c []\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\n\n#define MAX_LEN 50\n\ntypedef struct {\n    int val;\n    int count;\n} nodeEntry;\n\nint cmp(const void *a, const void *b) {\n    nodeEntry *node1 = (nodeEntry*)a;\n    nodeEntry *node2 = (nodeEntry*)b;\n\n    if (node1->count != node2->count) {\n        return node2->count - node1->count;\n    }\n\n    return node2->val - node1->val;\n}\n\nint calcXSum(int *nums, int left, int right, int x) {\n    int size = right - left + 1;\n    int nodeCount = 0, sum = 0;\n    nodeEntry nodes[size];\n\n    if (size <= x) {\n        for (int i = left; i <= right; ++i) {\n            sum += nums[i];\n        }\n\n        return sum;\n    }\n\n    for (int i = left; i <= right; ++i) {\n        bool found = false;\n        for (int j = 0; j < nodeCount; ++j) {\n            if (nodes[j].val == nums[i]) {\n                nodes[j].count +=1;\n                found = true;\n                break;\n            }\n        }\n\n        if (!found) {\n            nodes[nodeCount].val = nums[i];\n            nodes[nodeCount].count = 1;\n            ++nodeCount;\n        }\n    }\n\n    qsort(nodes, nodeCount, sizeof(nodeEntry), cmp);\n    for (int i = 0; i < x && i < nodeCount; ++i) {\n        sum += (nodes[i].val * nodes[i].count);\n    }\n\n    return sum;\n}\n\nint* findXSum(int* nums, int numsSize, int k, int x, int* returnSize) {\n    *returnSize = numsSize - k + 1;\n    int *ans = calloc(*returnSize, sizeof(int));\n    int idx = 0;\n\n    int left = 0, right = 0, sum = 0;\n    while (right < numsSize) {\n        if (right - left + 1 == k) {\n            sum = calcXSum(nums, left, right, x);\n            \n            ans[idx++] = sum;\n            sum = 0;\n\n            if (idx == *returnSize) break;\n            ++left;\n        }\n\n        ++right;\n    }\n\n    return ans;\n}\n\n```
['Array', 'Hash Table', 'C', 'Sliding Window', 'Sorting']