complex but easy to understand in python solution
\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        dp=[0]\n        for i in range(len(nums)):\n            if nums[i]==1:\n    
```\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        dp=[0]\n        for i in range(len(nums)):\n            if nums[i]==1:\n                dp.append(i)\n        dp2=[0]*(len(dp))\n        dp2[0]=dp[0]\n        for i in range(1,len(dp)):\n            dp2[i]=dp2[i-1]+dp[i]\n        li=[0]*(k//2+1)\n        li[0]=0\n        for i in range(1,(k//2)+1):\n            li[i]=li[i-1]+i\n        i=1\n        j=k\n        min_value=float("inf")\n        while(j<len(dp)):\n            half=(j+i)//2\n            value1=abs(dp[half]*(half-i)-(dp2[half-1]-dp2[i-1]))+abs((dp2[j]-dp2[half])-dp[half]*(j-half))\n            if k%2!=0:\n                min_value=min(min_value,value1-(li[-1]*2))\n            else:\n                value2=abs(dp[half+1]*(half+1-i)-(dp2[half]-dp2[i-1]))+abs((dp2[j]-dp2[half+1])-dp[half+1]*(j-(half+1)))\n                min_value=min(min_value,value1-(li[-1]+li[-2]),value2-(li[-1]+li[-2]))\n            j+=1\n            i+=1\n        return min_value\n\n```
[]