Buffer Manager

Time limit1sMemory limit128 MB

Summary
Given buffer states (free, digit worthiness, or locked) in a string, find the starting position of the K-length window without locked buffers whose digit sum is smallest.
Level

Medium4 of 10

Topics
Sliding window, Prefix sum, Implementation, String
Solved
No attempts yet

Problem

A DBMS (Database Management System) team has built an efficient Lock Manager and now needs a Buffer Manager.

Data blocks read from the hard drive are stored in main memory in a fixed number of pre-allocated buffers. Each buffer holds exactly one data block and is in one of three states:

  • free — it holds no useful data; its worthiness is 00.
  • occupied — it holds data with an integer worthiness from 11 to 99.
  • locked — it is in use by some part of the DBMS, so it can be neither reused nor flushed, and its worthiness is undefined.

When the DBMS reads a data block it must pick a buffer to store it. A free buffer is preferred; if none is free, an occupied non-locked buffer must be flushed. Maximum performance is achieved when several consecutive data blocks are read into consecutive memory buffers, so the manager always allocates one contiguous run of buffers.

Buffers are numbered from 11 to NN (1≤N≤1000001 \le N \le 100000). Given a request to read KK data blocks (1≤K≤100001 \le K \le 10000), the Buffer Manager must choose KK consecutive non-locked buffers numbered L,L+1,…,L+K−1L, L+1, \dots, L+K-1 whose total worthiness (the cost of flushing them) is as small as possible. If several starting positions give the same minimal total, choose the smallest LL. If no run of KK consecutive non-locked buffers exists — which also happens when N<KN < K — the request is impossible.

Write a program that processes one such request.

Input

The first line contains two integers NN and KK separated by a space.

The buffer states follow, one character per buffer:

  • 0 — the buffer is free.
  • 1–9 — the buffer is occupied and has that worthiness.
  • * — the buffer is locked.

These NN characters are written without spaces, grouped 8080 per line. Every line after the first contains exactly 8080 characters, except possibly the last.

Output

Print a single integer LL: the starting buffer number of the chosen run of KK consecutive non-locked buffers with the minimal total worthiness. If several starting positions tie, print the smallest LL.

Print 00 if it is impossible to find KK consecutive non-locked buffers.

Examples2

  1. Example 1

    Input
    100 53
    2165745216091853477755800393859785807207523169954341**7363*9*94664808*4777717089
    09825185827659480548
    
    Expected output
    0
    
  2. Example 2

    Input
    100 10
    2165745216091853477755800393859785807207523169954341**7363*9*94664808*4777717089
    09825185827659480548
    
    Expected output
    36