6 ms Beats 96.59%
\n\n# Code\njava []\nclass Solution {\n    public long minimumMoves(int[] nums, int k, int maxChanges) {\n        int start = 0;\n        while(start < nums.len
\n\n# Code\n```java []\nclass Solution {\n    public long minimumMoves(int[] nums, int k, int maxChanges) {\n        int start = 0;\n        while(start < nums.length && nums[start]!=1) start++;\n        int n = nums.length;\n        long ans = Long.MAX_VALUE;\n        if(maxChanges >= k-3) {\n            int longest = 0;\n            int idx = 0;\n            while(idx < n) {\n                int cnt = 0;\n                while(idx < n && nums[idx]==1) {\n                    idx++;\n                    cnt++;\n                }\n                idx++;\n                longest = Math.max(longest, cnt);\n            }\n            if(maxChanges>=k) {\n                if(longest == 0) return 2*k;\n                else if(longest == 1) return 2*k-2;\n                else if(longest == 2) {\n                    if(k==1) return 0;\n                    else return k*2-3;\n                }\n                else if(longest >= 3) {\n                    if(k == 1) return 0;\n                    else if(k==2) return 1;\n                    else return k*2-4;\n                }\n            } else if(maxChanges==k-1) {\n                if(longest == 1) return 2*k-2;\n                else if(longest == 2) {\n                    if(k==1) return 0;\n                    else return k*2-3;\n                }\n                else if(longest >= 3) {\n                    if(k == 1) return 0;\n                    else if(k==2) return 1;\n                    else return k*2-4;\n                }\n            } else if(maxChanges==k-2) {\n                if(longest == 2) {\n                    return k*2-3;\n                }\n                else if(longest >= 3) {\n                    if(k == 1) return 0;\n                    else if(k==2) return 1;\n                    else return k*2-4;\n                }\n            } else if(maxChanges==k-3) {\n                if(longest >= 3) {\n                    if(k == 1) return 0;\n                    else if(k==2) return 1;\n                    else return k*2-4;\n                }\n            }\n        }\n        long[] arr = new long[n];\n        int lcnt = 0;\n        int rcnt = 0;\n        int lidx = start;\n        int ridx = start;\n        long tot = 0;\n        int num = k - maxChanges;\n        while(rcnt < num) {\n            if(nums[ridx]==1) {\n                rcnt++;\n                tot += ridx-start;\n            }\n            ridx++;\n        }\n        int nextIdx = ridx;\n        ridx--;\n        while(nextIdx < n && nums[nextIdx]!=1) nextIdx++;\n        for(int i = start; i < n; i++) {\n            if(i == start) {\n                arr[i] = tot;\n                lcnt++;\n                rcnt--;\n                continue;\n            }\n            tot += lcnt - rcnt;\n            if(nums[i] == 1) {\n                lcnt++;\n                rcnt--;\n            }\n            if(i-lidx >= nextIdx-i && nextIdx != n) {\n                tot = tot - (i-lidx) + nextIdx-i;\n                lcnt--;\n                lidx++;\n                while(lidx < n && nums[lidx]==0) lidx++;\n                rcnt++;\n                ridx = nextIdx;\n                nextIdx++;\n                while(nextIdx < n && nums[nextIdx]!=1) nextIdx++;\n            }\n            arr[i] = tot;\n        }\n        for(int i = start; i < n; i++) {\n            ans = Math.min(ans, arr[i] + 2*maxChanges);\n        }\n\n        return ans;\n    }\n}\n```
['Java']