ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests

1061. Buffer Manager

Time limit: 0.5 second
Memory limit: 64 MB
Data blocks being read by DBMS from the hard drive are stored in the main memory in a fixed number of pre-allocated buffers. Each buffer can hold one data block. Each buffer can be either free (does not contain any useful information) or occupied by some data. When DBMS is going to read data block from the hard drive it has to decide which buffer to use for data storing. If there are any free buffers, then one of them is used for that purpose. If there are no free buffers, then one of the occupied buffers has to be flushed to become free, unless it was locked by some part of DBMS.
The choice of the buffer to flush is critical to DBMS performance. A lot of different choice algorithms were developed. Your DBMS is going to implement the Advanced Buffer Management algorithm which reads a number of consecutive data blocks from the hard drive into consecutive memory buffers.
Buffers are numbered from 1 to N, where N is a total number of buffers. Each buffer can be in any one of the following states: free, occupied or locked. Each occupied buffer is assigned an integer from 1 to 9 – the worthiness of the currently stored information in that buffer. The worthiness of free buffers is considered to be zero. Locked buffers cannot be neither used nor flushed and their worthiness is undefined.
Having received the request to read K data blocks from the hard drive, Buffer Manager has to choose K consecutive (i.e. numbered from L to L + K − 1) non-locked buffers with minimal possible sum of their worthiness, or to report that it is impossible. The latter can happen if some buffers are locked or total number of buffers is less than K.
Your task is to write a program that models the processing of one request to Buffer Manager using the above algorithm.


The first line contains integers N and K (1 ≤ N ≤ 100000; 1 ≤ K ≤ 10000).
Starting from the second line there is a description of a buffers' state. The state of each buffer is represented by a single character:
  • 0 – when the corresponding buffer is free.
  • 1 – when the corresponding buffer is occupied and has worthiness of 1.
  • 2 – when the corresponding buffer is occupied and has worthiness of 2.
  • ...
  • 9 – when the corresponding buffer is occupied and has worthiness of 9.
  • * – when the corresponding buffer is locked.
Those characters are situated on the consecutive lines grouped by 80 characters per line without any spaces. Thus, each line starting from the second one contains exactly 80 characters with a possible exception for the last line.


Output the integer L that is the buffer number where first of the K blocks from the hard drive shall be read to ensure the minimal possible total worthiness of the blocks that have to be flushed. If there are more than one such value for L, then output the smallest one.
Output a single number 0 if it's impossible to find K consecutive non-locked buffers.


100 53
100 10
Problem Source: 2000-2001 ACM Northeastern European Regional Programming Contest