PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000200. Tổng của các giá trị xuất hiện nhiều nhất trên mỗi cửa sổ (Find X-Sum of All K-Long Subarrays II)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng của các giá trị xuất hiện nhiều nhất trên mỗi cửa sổ (Find X-Sum of All K-Long Subarrays II)

Find X-Sum of All K-Long Subarrays II

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of positive integers a1,a2,…,ana_1,a_2,\ldots,a_n and two integers k,xk,x.

Consider a contiguous subarray containing exactly kk elements. For each distinct value vv occurring in the subarray, let f(v)f(v) denote its frequency.

Rank distinct values in descending order of f(v)f(v), breaking ties by favoring the larger numerical value.

Keep all occurrences of the first xx distinct values in this ranking and sum the retained elements. If the subarray contains fewer than xx distinct values, retain every element. The resulting sum is called its xx-sum.

For every starting position i=1,2,…,n−k+1i=1,2,\ldots,n-k+1, calculate the xx-sum of ai,ai+1,…,ai+k−1a_i,a_{i+1},\ldots,a_{i+k-1} and print these answers in increasing order of ii.

Input

  • The first line contains three integers n,k,xn,k,x: the array length, window length, and number of top distinct values.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, in their original order.

Output

Print exactly n−k+1n-k+1 integers on one line, separated by spaces. Integer ii is the xx-sum of the window beginning at position ii.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; 1≤x≤k≤n1\le x\le k\le n; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤x≤k≤n1\le x\le k\le n; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤x≤k≤n1\le x\le k\le n; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

8 6 2
1 1 2 2 3 4 2 3

Output

6 10 12

Explanation

There are n−k+1=3n-k+1=3 windows.

  • For [1,1,2,2,3,4][1,1,2,2,3,4], values 11 and 22 each occur twice, while 33 and 44 occur once. Keeping both copies of 11 and both copies of 22 gives 66.
  • For [1,2,2,3,4,2][1,2,2,3,4,2], value 22 occurs three times. Values 1,3,41,3,4 each occur once, so the larger value 44 wins the tie. The retained elements 2,2,2,42,2,2,4 sum to 1010.
  • For [2,2,3,4,2,3][2,2,3,4,2,3], value 22 occurs three times, value 33 twice, and value 44 once. Keeping all 22s and 33s gives 1212.

Hence the answers are 6 10 12.