One of the most understandable approach w/o window
Approach\n Describe your approach to solving the problem. \nGet the minimal cost by naively search pivot (= dylan\'s index we choose).\n\nBut how to efficiently
# Approach\n<!-- Describe your approach to solving the problem. -->\nGet the minimal cost by naively search pivot (= dylan\'s index we choose).\n\nBut how to efficiently search the cost gien a pivot? Here, we need to figure out stratgy.\n\n1. If there\'s 1 is adjacent to pivot, pick it up first. (cost = index - pivot)\n2. If quota of maxChange is enough, we generate 1 adjacent to pivot and pick it up. (cost = 2 per each operation)\n3. If there\'s no quota can change from 0 to 1, and we still have to pick up $k$ times. We need to find a interval that the size is $k$ and have the minimal cost. \n    * Obviously, the order of picking up a number is depending on the distance from pivot.\n    * We assume $\\delta$ be the abs(number that last picking up - pivot), we can use binary search to find out what $\\delta$ should be that can pick up $k$ times.\n    * After having a range that we need to pick up, we can calculate $\\sum_i |i-pivot|$ by tearing this into two parts. $(\\sum_{i, i<pivot} pivot - i) + (\\sum_{i, i\\ge pivot} i - pivot)$\n    * This can be done by prefix sum technique. For example, if you need to pick up the number whose indices are (0, 1, 4) and pivot is $5$, you can calculate the cost $5 \\times len(3) - sum(0, 1, 4)$.\n\n# Complexity\n- Time complexity: $$O(n \\log^2 n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Time complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python\nimport bisect\nclass Solution:\n    def minimumMoves(self, nums: List[int], k: int, maxChanges: int) -> int:\n        # Special Case:\n        if sum(nums) == 0:\n            return k * 2\n        index1 = list(i for i, num in enumerate(nums) if num)\n        sumL = [0]\n        for num in index1: sumL.append(sumL[-1] + num)\n        def inter_sum(l, r):\n            return sumL[r+1] - sumL[l]\n        def cost(dylanIndex, k):\n            def trial(delta):\n                L = bisect.bisect_left(index1, dylanIndex - delta)\n                R = bisect.bisect_left(index1, dylanIndex + delta)\n                if R == len(index1) or index1[R] != dylanIndex + delta:\n                    R -= 1\n                return R - L + 1, (L, R)\n            L, R = -1, 10**5\n            while R - L != 1:\n                M = (L + R + 1) // 2\n                if trial(M)[0] >= k:\n                    R = M\n                else:\n                    L = M\n            delta, chosen = R, trial(R)[1]\n            if chosen[1] - chosen[0] + 1 > k: # Avoid picking up k+1 nums\n                chosen = [chosen[0], chosen[1]-1]\n            pivot = bisect.bisect_left(index1, dylanIndex)\n            # Calculate sum(dylanIndex - index1[L ~ pivot-1])\n            Lcost = dylanIndex * (pivot - chosen[0]) - inter_sum(chosen[0], pivot-1)\n            # Calculate sum(index1[pivot ~ R] - dylanIndex)\n            Rcost = max(0, inter_sum(pivot, chosen[1]) - dylanIndex * (chosen[1] - pivot + 1))\n            return Lcost + Rcost\n\n        ans = float(\'inf\')\n        for dylanIndex in range(len(nums)):\n            near = sum(nums[max(0, dylanIndex-1):dylanIndex+2])\n            byChange = 0 if near >= k else min(maxChanges, k - near)\n            curCost = cost(dylanIndex, k - byChange)\n            ans = min(ans, curCost + byChange * 2)\n        return ans\n```
['Binary Search', 'Suffix Array', 'Python3']