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

#FT0000047. Chi phí cửa sổ trượt (Sliding Window Cost)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Chi phí cửa sổ trượt (Sliding Window Cost)

Sliding Window Cost

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given integers x1,x2,…,xnx_1,x_2,\ldots,x_n and window length kk, consider every contiguous window containing exactly kk values, from left to right.

For each window, change its values to one common integer by increasing or decreasing individual elements. Changing uu into vv costs ∣u−v∣|u-v|, and the total cost is the sum over the window. Find the minimum possible total cost for every window. Windows are evaluated independently; hypothetical changes in one window do not alter the original array.

Input

  • Line 1: integers n,kn,k, array length and window length.
  • Line 2: exactly nn integers x1,x2,…,xnx_1,x_2,\ldots,x_n.

Output

Print n−k+1n-k+1 integers on one line, the minimum costs for each successive window.

Subtasks

Subtask 1 (20 points):

  • 1≤k≤n≤201\le k\le n\le 20; 1≤xi≤1091\le x_i\le 10^9 for every 1≤i≤n1\le i\le n.

Subtask 2 (30 points):

  • 1≤k≤n≤20001\le k\le n\le 2000; 1≤xi≤1091\le x_i\le 10^9 for every 1≤i≤n1\le i\le n.

Subtask 3 (50 points):

  • 1≤k≤n≤2000001\le k\le n\le 200000; 1≤xi≤1091\le x_i\le 10^9 for every 1≤i≤n1\le i\le n.

Examples

Examples 1

Input

8 3
2 4 3 5 8 1 2 1

Output

2 2 5 7 7 1

Explanation

For [2,4,3][2,4,3], choosing common value 33 costs 1+1+0=21+1+0=2. Subsequent windows choose medians 4,5,5,2,14,5,5,2,1, yielding costs 2,5,7,7,12,5,7,7,1. A median minimizes total absolute deviations.

Examples 2

Input

4 2
5 5 1 9

Output

0 4 8

Explanation

The values in [5,5][5,5] already agree, so the cost is zero. For [5,1][5,1], every common value between 11 and 55 gives cost 44. For [1,9][1,9], the minimum cost is 88.

For a pair u,vu,v, choosing a common target between them gives total cost ∣u−v∣|u-v|. Thus the windows [5,5][5,5], [5,1][5,1], and [1,9][1,9] contribute ∣5−5∣=0|5-5|=0, ∣5−1∣=4|5-1|=4, and ∣1−9∣=8|1-9|=8, respectively.