Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def main(argv):
- path = argv[0]
- f = open(path)
- n, k = map(int, f.readline().split())
- s = f.readline().rstrip()
- if n <= 3:
- print(n)
- return
- cnt = [0] * (n - 2)
- cnt[0] += int(s[0] == "S") + int(s[1] == "S")
- cnt[1] += int(s[1] == "S")
- cnt[-1] += int(s[-1] == "S") + int(s[-2] == "S")
- cnt[-2] += int(s[-2] == "S")
- for i in range(2, n - 2):
- m = int(s[i] == "S")
- cnt[i - 2] += m
- cnt[i - 1] += m
- cnt[i] += m
- first_sum = sum([c for c in cnt[::3]])
- second_sum = sum([c for c in cnt[1::3]])
- third_sum = sum([c for c in cnt[2::3]])
- idx_search_order = []
- if first_sum >= second_sum >= third_sum:
- idx_search_order = [0, 1, 2]
- elif first_sum >= third_sum >= second_sum:
- idx_search_order = [0, 2, 1]
- elif second_sum >= first_sum >= third_sum:
- idx_search_order = [1, 0, 2]
- elif second_sum >= third_sum >= first_sum:
- idx_search_order = [1, 2, 0]
- elif third_sum >= first_sum >= second_sum:
- idx_search_order = [2, 0, 1]
- else:
- idx_search_order = [2, 1, 0]
- cnt_i = {0: set(), 1: set(), 2: set(), 3: set()}
- for i, c in enumerate(cnt):
- cnt_i[c].add(i)
- ans = s.count(".")
- for c in range(3, 0, -1):
- while cnt_i[c] and k > 0:
- for offset in idx_search_order:
- this_cnt_i = cnt_i[c] & {i for i in range(offset, n - 2, 3)}
- while this_cnt_i:
- i = this_cnt_i.pop()
- this_cnt_i -= {i - 2, i - 1, i, i + 1, i + 2}
- cnt_i[c] -= {i - 2, i - 1, i, i + 1, i + 2}
- ans += c
- k -= 1
- print(ans)
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement