Freq count + 2 pointer sliding window||15 ms Beats 94.74%
Intuition\n Describe your first thoughts on how to solve this problem. \nUse 2 pointers/sliding window to solve.\n\nOnly the testcases are small, it\'s not easy
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse 2 pointers/sliding window to solve.\n\nOnly the testcases are small, it\'s not easy.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. define a function `int x_sum( const auto& freq, int k, int x)`  to compute the x-sum; the most time consuming part is the copying freq & sort.\n2. Compute ans[0], i.e x_sum for the 0th window\n3. Sliding window with moving l, r to compute x_sum for other windows\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n \\times 51 \\log 51)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$O(51)$\n# Code||C++ 15 ms Beats 94.74%\n```cpp []\nclass Solution {\npublic:\n    using int2=pair<int, int>;\n    int x_sum( const auto& freq, int k, int x){\n        auto freq2=freq;// copy\n        sort(freq2.begin(), freq2.end(), greater<int2>());\n        int sum=0;\n        for (int i=0; i<x; i++){\n            auto [f, num]=freq2[i];\n            if (f==0) break;\n            sum+=num*f;\n        }\n        return sum;\n    }\n    vector<int> findXSum(vector<int>& nums, int k, int x) {\n        const int n=nums.size(), sz=n-k+1;\n        vector<int> ans(sz);\n        array<int2, 51> freq;\n        freq.fill({0, 0});\n        for(int r=0; r<k; r++){\n            int z=nums[r];\n            freq[z].second=z;\n            freq[z].first++;\n        }\n        ans[0]=x_sum(freq, k, x);\n        for(int l=1, r=k; l<sz; l++, r++){\n            int L=nums[l-1], R=nums[r];\n            freq[L].first--;\n            freq[R].first++;\n            freq[R].second = R;\n            ans[l]=x_sum(freq, k, x);\n        }\n        return ans;\n    }\n};\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```
['Two Pointers', 'Sliding Window', 'C++']