Median theory. Naive optimisation over brute force with binary search.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    \n    long long doTask(vector<int> & nums, \n                     vector<int> & p_sum, \n                     vector<long long> & p_index_sum, \n                     long long ops, \n                     long long i, \n                     long long left_req, \n                     long long right_req) {\n        int n = nums.size();\n        \n        if(nums[i]) {\n                left_req ++;\n                right_req ++;\n            } \n            \n            if(i > 0 && nums[i - 1]) {\n                left_req ++;\n            }\n            \n            if(i < n - 1 && nums[i + 1]) {\n                right_req ++;\n            }\n            \n            \n            int start = 1, end = i + 1, posL = -1, posR = -1;\n            \n            while(start <= end) {\n                int mid = (start + end) / 2;\n                \n                if(p_sum[i + 1] - p_sum[mid - 1] >= left_req) {\n                    posL = mid;\n                    start = mid + 1; // as close as possible.\n                } else {\n                    end = mid - 1;\n                }\n            }\n            \n            \n            \n            \n            start = i + 1; end = n;\n            \n            while(start <= end) {\n                int mid = (start + end) / 2;\n                \n                if(p_sum[mid] - p_sum[i] >= right_req) {\n                    posR = mid;\n                    end = mid - 1; // as close as possible.\n                } else {\n                    start = mid + 1;\n                }\n            }\n            \n            \n            // now formulas.\n            // you have everything in place.\n            \n            \n            if(posL == -1 || posR == -1) return 1e18;\n            \n            \n            \n            ops +=  left_req * i * 1LL - (p_index_sum[i + 1] - p_index_sum[posL - 1]);\n            \n            if(i > 0 && nums[i - 1]) {\n                ops --;\n            }\n            \n            ops +=  (p_index_sum[posR] - p_index_sum[i]) - right_req * i * 1LL;\n            \n            if(i < n - 1 && nums[i + 1]) {\n                ops --;\n            }\n        \n        return ops;\n        \n    }\n    \n    \n    long long minimumMoves(vector<int>& nums, int k, int maxChanges) {\n        int n = nums.size();\n        \n        vector<long long> p_index_sum(n + 1);\n        vector<int> p_sum(n + 1);\n        \n        for(int i = 0; i < n; i ++) {\n            p_index_sum[i + 1] = p_index_sum[i];\n            p_sum[i + 1] = p_sum[i];\n            if(nums[i]) {\n                p_index_sum[i + 1] += i;\n                p_sum[i + 1] += 1; // binary search will be applied on this.\n            }\n        }\n        \n        long long ans = 1e18;\n        \n        for(int i = 0; i < n; i ++) {\n            // if i index is our pivot or dylan index.\n            \n            int req = k;\n            long long ops = 0;\n            \n            if(nums[i]) {\n                req --;\n            }\n            \n            if(req > 0 && i > 0 && nums[i - 1]) {\n                ops ++;\n                req --;\n            }\n            \n            if(req > 0 && i < n - 1 && nums[i + 1]) {\n                ops ++;\n                req --;\n            }\n            \n            \n            int doChangesAndUse = min(maxChanges, req);\n            req -= doChangesAndUse;\n            ops += 2 * doChangesAndUse;\n            \n            if(req == 0) {\n                ans = min(ans, ops);\n                continue;\n            }\n            \n            \n            // use median theory and achieve req. easily via binary search cleanly.\n            \n            int left_req = req / 2;\n            int right_req = req - (req/2);\n            \n            \n            \n            \n            ans = min(ans, doTask(nums, p_sum, p_index_sum, ops, i, left_req, right_req));\n            ans = min(ans, doTask(nums, p_sum, p_index_sum, ops, i, right_req, left_req));\n        }\n        \n        return ans;\n        \n    }\n};\n```
['C++']