Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CodeForces
- {
- class Program
- {
- public static void Main()
- {
- string input = Console.ReadLine();
- int n = Convert.ToInt32(input.Split()[0]);
- int m = Convert.ToInt32(input.Split()[1]);
- int time = 0;
- int startIndex = 0;
- int c = 0;
- int rightStairs = m + 1;
- string[] str = new string[n];
- for (int i = n - 1; i >= 0; i--)
- {
- str[i] = Console.ReadLine();
- }
- for (int i = 0; i < n; i++)
- {
- if (startIndex == 0)
- {
- int tempTime = time;
- input = str[i];
- int indexOfChar = input.IndexOf('1', startIndex);
- while (indexOfChar != -1)
- {
- time += indexOfChar - startIndex;
- startIndex = indexOfChar + 1;
- indexOfChar = input.IndexOf('1', startIndex);
- startIndex--;
- }
- if (tempTime == time)
- {
- time += -c - 1;
- break;
- }
- else if (i + 1 == n)
- {
- break;
- }
- if (rightStairs - startIndex < startIndex)
- {
- time += rightStairs - startIndex;
- c = rightStairs - startIndex;
- startIndex = rightStairs;
- }
- else
- {
- time += startIndex;
- c = startIndex;
- startIndex = 0;
- }
- time++;
- }
- else
- {
- int tempTime = time;
- input = str[i];
- int indexOfChar = input.LastIndexOf('1', startIndex);
- while (indexOfChar != -1)
- {
- time += startIndex - indexOfChar;
- startIndex = indexOfChar - 1;
- indexOfChar = input.LastIndexOf('1', startIndex);
- startIndex++;
- }
- if (tempTime == time)
- {
- time += -c - 1;
- break;
- }
- else if (i + 1 == n)
- {
- break;
- }
- else if (rightStairs - startIndex < startIndex)
- {
- time += rightStairs - startIndex;
- c = rightStairs - startIndex;
- startIndex = rightStairs;
- }
- else
- {
- time += startIndex;
- c = startIndex;
- startIndex = 0;
- }
- time++;
- }
- }
- if (time != -1)
- Console.WriteLine(time);
- else Console.WriteLine(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement