values of length n and two integers width and top.
For any segment of the array, define its **frequency sum** as follows:
1. Count how many times each value occurs.
2. Rank the distinct values by frequency; on a tie, the larger value ranks higher.
3. Keep only the top highest-ranked values (all of their occurrences) and add up the kept elements.
If the segment has fewer than top distinct values, its frequency sum is simply the sum of the segment.
Return an array of n - width + 1 numbers whose i-th entry is the frequency sum of values[i..i + width - 1].
The array can hold up to 10⁵ elements, so recounting every window from scratch is too slow.values=[4, 4, 1, 2, 2, 7], width=4, top=2 → [10, 8, 11]
In [4, 4, 1, 2] the value 4 is the most frequent (twice), and between 1 and 2, seen once each, the larger 2 wins: 4 + 4 + 2 = 10. In [4, 1, 2, 2] we keep 2, 2 and 4, for 8. In [1, 2, 2, 7] we keep 2, 2 and 7, for 11.
values=[9, 3, 9], width=3, top=3 → [21]
There are only two distinct values, fewer than top = 3, so the frequency sum is the whole window: 21.
values=[4, 4, 1, 2, 2, 7], width=4, top=2
[10, 8, 11]
Run checks the first 3 tests in your browser. Submit checks all 196 tests on the server, hidden ones included.