✅C++ beats 100%. Intuition and approach + comments in code
Intuition\nIntuition 1\nThe cost of using operation 1 to pick 1, is 2.\nThe cost of using operation 2 to pick 1, is the distance to that 1.\n\n\nIntuition 2\ni 
# Intuition\n**Intuition 1**\nThe cost of using operation 1 to pick 1, is 2.\nThe cost of using operation 2 to pick 1, is the distance to that 1.\n\n\n**Intuition 2**\ni - 1, i, i + 1, at most 3 places have diatance <= 2.\nWe will at most take m + 3 of 1 using operation 2,\n\nand also,\nm <= n, where we have n of 1 at the begin.\nm <= k, where need k of 1.\n\n\n# Approach\n\nNow the question is simple,\nwhat\'s the minimum distance from m points in A?\n\nWe choose the center of m points as initial index,\nand calculate the total distance with prefix skill.\n\nReturn the minimum result in the end.\n\n1. Check for duos and triplets of 1 (cheaper thant step 1)\n2. Can we only use duos and triplets + MaxChanges?\n3. Use as many maxChanges as possible. Adjust k\n4. Find best start position (sliding window of adjusted-k ones)\n5. Within each window, pick the median to start and compute cost in O(1) with prefix sums\n\n# Complexity\n- Time complexity: $$O(n)$$ \n![image.png](https://assets.leetcode.com/users/images/430f5c1b-b8d1-4a38-be52-f19649cdc708_1710648844.7809124.png)\n- Space complexity: $$O(n)$$ \n\n\n# Code\n```\n\n#define sz(c) ((int) c.size())\n#define forn(i,n) for(int i=0;i<(int)(n);i++)\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntemplate <typename T> void mmin(T& a, const T& b) {\n    a = (a) < (b) ? (a) : (b);\n}\nclass Solution {\npublic:\n    long long minimumMoves(vector<int>& a, int k, int mc) {\n        int n = sz(a);\n        vi ones; forn(i, n) if(a[i]) ones.push_back(i); // indices with a[i] = 1;\n        if(ones.empty()) return 2*k;\n        int duo = -1, trio = -1; // Check for duos and triplets of 1 (cheaper thant step 1)\n        forn(i, n-1) if(a[i] and a[i+1]) duo = i;\n        forn(i, n-2) if(a[i]+a[i+1]+a[i+2] == 3) trio = i;\n        int hi = 1; if(duo != -1) hi = 2; if(trio != -1) hi = 3;\n        if(mc + hi >= k){ // Can we only use duos and triplets + MaxChanges?\n            mmin(hi, k);\n            k -= hi;\n            return hi-1 + 2*k;\n        }\n        int m = sz(ones);\n        vll p(m+1); forn(i, m) p[i+1] = p[i] + ones[i];\n        k -= mc; // Use as many maxChanges as possible. Adjust k\n        ll precost = 2*mc;\n        ll ans = 1e18;\n        forn(l, m-k+1){ // Sliding window of length = k in array ones\n            int r = l+k-1;\n            int im = (r + l) / 2;\n            ll mid = ones[im]; // median\n            ll lsum = p[im+1] - p[l]; // sum ones[l, im]\n            ll rsum = p[r+1] - p[im]; // sum ones[im, r]\n            // compute cost of getting all ones\n            // need |y - x| moves to take 1 point\n            ll left = mid * (im-l+1) - lsum;\n            ll right = rsum - mid * (r-im+1);\n            mmin(ans, precost + left + right);\n        }\n        return ans;\n    }\n};\n\n```
['C++']