import java.util.*; import java.io.*; public class ducks_font { public static void main(String[] args) throws Exception { PrintWriter out = new PrintWriter(System.out); new ducks_font(new FastScanner(System.in), out); out.close(); } /* Key insight: Toggling these states: 11111111111 111111111111 Is equivalent to this toggling: 111111 1111111 Never have to consider overlapping toggling. Solution can still be written to brute force this though. (Though it would be easy to see how costs remap.) */ boolean[] isDuck; int oo = 987654321; int lengthThreshold; // Return the minimum number of wand uses to meet the criteria. // Currently running a sequence of ducks (must reach lengthThreshold). // // State: // wandActive: 0 - inactive, 1 - active // sequencesNeeded: [0, minSequences] // lengthLeft: [0, lengthThreshold] // // position: position in the string // // State space: O(n * k * |s|) Integer[][][][] memo2; int go2(int wandActive, int lengthLeft, int sequencesNeeded, int position) { if (lengthLeft == 0) return go1(wandActive, sequencesNeeded, position); if (position == isDuck.length) return oo; if (memo2[wandActive][lengthLeft][sequencesNeeded][position] != null) return memo2[wandActive][lengthLeft][sequencesNeeded][position]; int res = oo; boolean curPosIsDuck = isDuck[position]; if (wandActive == 1) curPosIsDuck = !isDuck[position]; // This can only continue if the current position is a duck if (curPosIsDuck) { int r1 = go2(wandActive, lengthLeft-1, sequencesNeeded, position+1); int r2 = go2(1-wandActive, lengthLeft-1, sequencesNeeded, position+1)+1-wandActive; res = Math.min(r1, r2); } return memo2[wandActive][lengthLeft][sequencesNeeded][position] = res; } // Return minimum number of wand uses to meet the criteria. // Currently not running a sequence of ducks. // // State: // wandActive: 0 - inactive, 1 - active // sequencesNeeded: [0, minSequences] // position: position in the string // // State space: O(n * |s|) // Integer[][][] memo1; int go1(int wandActive, int sequencesNeeded, int position) { if (sequencesNeeded == 0) return 0; if (position == isDuck.length) return oo; if (memo1[wandActive][sequencesNeeded][position] != null) return memo1[wandActive][sequencesNeeded][position]; int res = oo; boolean curPosIsDuck = isDuck[position]; if (wandActive == 1) curPosIsDuck = !isDuck[position]; if (!curPosIsDuck) { // Consider starting a new run of ducks! // (Keep wand state or toggle it too.) int r1 = go2(wandActive, lengthThreshold, sequencesNeeded-1, position+1); int r2 = go2(1-wandActive, lengthThreshold, sequencesNeeded-1, position+1) + 1-wandActive; int rr = Math.min(r1, r2); res = Math.min(res, rr); } // Continue moving. Don't care about forming a duck line. // Try both toggling and untoggling the wand. int r1 = go1(wandActive, sequencesNeeded, position+1); int r2 = go1(1-wandActive, sequencesNeeded, position+1) + 1-wandActive; int rr = Math.min(r1, r2); res = Math.min(res, rr); return memo1[wandActive][sequencesNeeded][position] = res; } public ducks_font(FastScanner in, PrintWriter out) { lengthThreshold = in.nextInt(); int numSequences = in.nextInt(); String s = in.next(); // Can this many sequences fit in the string? int minSpacesRequired = lengthThreshold * numSequences + (numSequences-1); if (minSpacesRequired > s.length()) { out.println(-1); return; } isDuck = new boolean[s.length()]; for (int i=0; i= numChars){ curChar = 0; try{ numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isSpaceChar(c)); return res.toString(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }