import sys n, k, t = map(int, sys.stdin.readline().split()) obw = sys.stdin.readline().strip() m=-1 # maks bajtogodzin # moze sobie zostac w domu if obw.count('1') <= k : s2 = min(obw.count('2'), k-obw.count('1')) # jakie(czy wogule) 2 moze skipowac if obw.count('1') + s2 <= k: m = obw.count('3') + obw.count('1') + s2 # no albo moze ma jechac do biura # slide two len=t windows for i in range(n-t +1): # i - left window pointer for j in range(i+t, n-t +1): # j - right window pointer home = obw[:i] + obw[j+t:] travel = obw[i:i+t] + obw[j:j+t] s = travel.count('1') + travel.count('2') + home.count('1') # travel + home office skip if s > k: # no po pracie mu bendzie continue # calc g and update m s2 = min(home.count('2'), k - s) # 2s he cn afford to skip if s + s2 > k: continue g = home.count('3') + home.count('1') + s2 # can solve dur 3 and 1 at home, also aff 2 m = max(m, g) print(m)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import sys n, k, t = map(int, sys.stdin.readline().split()) obw = sys.stdin.readline().strip() m=-1 # maks bajtogodzin # moze sobie zostac w domu if obw.count('1') <= k : s2 = min(obw.count('2'), k-obw.count('1')) # jakie(czy wogule) 2 moze skipowac if obw.count('1') + s2 <= k: m = obw.count('3') + obw.count('1') + s2 # no albo moze ma jechac do biura # slide two len=t windows for i in range(n-t +1): # i - left window pointer for j in range(i+t, n-t +1): # j - right window pointer home = obw[:i] + obw[j+t:] travel = obw[i:i+t] + obw[j:j+t] s = travel.count('1') + travel.count('2') + home.count('1') # travel + home office skip if s > k: # no po pracie mu bendzie continue # calc g and update m s2 = min(home.count('2'), k - s) # 2s he cn afford to skip if s + s2 > k: continue g = home.count('3') + home.count('1') + s2 # can solve dur 3 and 1 at home, also aff 2 m = max(m, g) print(m) |