<p>You are given an integer array, <code>nums</code>, and an integer <code>k</code>. <code>nums</code> comprises of only <code>0</code>&#39;s and <code>1</code>&#39;s. In one move, you can choose two <strong>adjacent</strong> indices and swap their values.</p>

<p>Return <em>the <strong>minimum</strong> number of moves required so that </em><code>nums</code><em> has </em><code>k</code><em> <strong>consecutive</strong> </em><code>1</code><em>&#39;s</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,<u>1</u>,<u>1</u>] and have 2 consecutive 1&#39;s.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,<u>1</u>,<u>1</u>,<u>1</u>].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0,1], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums already has 2 consecutive 1&#39;s.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is <code>0</code> or <code>1</code>.</li>
	<li><code>1 &lt;= k &lt;= sum(nums)</code></li>
</ul>

class Solution:
    def minMoves(self, nums: List[int], k: int) -> int:
        if(k  == 1): return 0
        """
        X00X0X000X0000X
        C[i] = num of 1 seen before i
        C[0] = 0
        C[1] = 1 = C[2] = C[3]
        C[4] = 2 = C[5]
        C[6] = 3 = C[7] = C[8]

        XXX000000X0000X
        D0(2) = (2 - 0) - (C[2] - C[0])
        D0(8) = (8 - 0) - (C[8] - C[0])

        DI(8, 2) = (8 - 2) - (C[8] - C[2])

        DI(3, 8) = (3 - 8) - (C[3] - C[8]) = -5 - (-2) = -3
        """
        N = len(nums)
        C1 = [0] * N
        C2 = [0] * N

        for i in range(1, N):
            C1[i] = C1[i-1] + 1 if (nums[i-1] == 1) else C1[i-1]
        
        for i in range(N-2, -1, -1):
            C2[i] = C2[i+1] + 1 if (nums[i+1] == 1) else C2[i+1]
        
        sorted_list = deque()

        # print(C1, C2)

        S1, S2 = 0, 0
        C11, C22 = 0, 0

        i = 0
        while(i < N):
            if(nums[i] == 1): sorted_list.append(i)
            i += 1
            if(len(sorted_list) == k): break
        
        M = (k - 1) // 2
        for j in range(M + 1):
            S1 += sorted_list[j]
            C11 += C1[sorted_list[j]]
        
        for j in range(M+1, k):
            S2 += sorted_list[j]
            C22 += C2[sorted_list[j]]

        # print(S1, S2, C11, C22)
        minimum_cost = ((M + 1) * sorted_list[M] - S1) - ((M+1) * C1[sorted_list[M]] - C11)
        minimum_cost += (S2 - (k-M-1) * sorted_list[M]) - ((k-M-1) * C2[sorted_list[M]] - C22)

        # print(minimum_cost)

        while(i < N):
            if(nums[i] == 1):
                midx = sorted_list[M + 1]
                S2 -= midx
                C22 -= C2[midx]
                S1 += midx
                C11 += C1[midx]

                idx = sorted_list.popleft()
                S1 -= idx
                C11 -= C1[idx]

                sorted_list.append(i)
                S2 += i
                C22 += C2[i]

                minimum_cost = min(
                    minimum_cost,
                    ((M + 1) * sorted_list[M] - S1) - ((M+1) * C1[sorted_list[M]] - C11) + (S2 - (k-M-1) * sorted_list[M]) - ((k-M-1) * C2[sorted_list[M]] - C22)
                )
            
            # print(minimum_cost)
            
            i += 1
    

        return minimum_cost
29400