import sys
class Journey:
def __init__(self, start, end, used_k):
self.start = start
self.end = end
self.used_k = used_k
def __repr__(self):
return (
f"START: {self.start}\n"
f"END: {self.end}\n"
f"USED K: {self.used_k}\n"
)
command = input().split(" ")
n = int(command[0])
k = int(command[1])
t = int(command[2])
sequence_str = input()
n1 = int(sequence_str.count('1'))
n2 = int(sequence_str.count('2'))
n3 = int(sequence_str.count('3'))
if k >= n1:
print(str(n3+min(k,n1+n2)))
sys.exit(0)
if k < n1 and 2 * t > n3 + k:
print(-1)
sys.exit(0)
#Drive to office possible patterns
possible_start_index = []
for x in range(0, n - 2*t):
possible = True
k_to_use = k
for y in range(0,t):
if sequence_str[x+y] != '3':
k_to_use-=1
if k_to_use >=0:
possible_start_index.append(Journey(x,x+t-1,k-k_to_use))
#Drive from office possible patterns
possible_end_index = []
for x in range(t, n-(t-1)):
possible = True
k_to_use = k
for y in range(0,t):
if sequence_str[x+y] != '3':
k_to_use-=1
if k_to_use >=0:
possible_end_index.append(Journey(x,x+t-1,k-k_to_use))
if len(possible_start_index) == 0 or len(possible_end_index) == 0:
print(-1)
sys.exit(0)
#Symulacja dla każdej kombinacji
max_days = -1
for s in range(0,len(possible_start_index)):
for e in range(0,len(possible_end_index)):
if possible_start_index[s].used_k + possible_end_index[e].used_k <= k and possible_start_index[s].end < possible_end_index[e].start:
current_days = 0
free_k = k - (possible_start_index[s].used_k + possible_end_index[e].used_k)
for i in range(0,n):
if i < possible_start_index[s].start or i > possible_end_index[e].end:
if sequence_str[i] == '3':
current_days+=1
elif sequence_str[i] == '1':
free_k-=1
if free_k<0:
current_days = -1
continue
if free_k>0 and current_days>-1:
current_days +=free_k
if current_days>max_days:
max_days = current_days
print(max_days)
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import sys class Journey: def __init__(self, start, end, used_k): self.start = start self.end = end self.used_k = used_k def __repr__(self): return ( f"START: {self.start}\n" f"END: {self.end}\n" f"USED K: {self.used_k}\n" ) command = input().split(" ") n = int(command[0]) k = int(command[1]) t = int(command[2]) sequence_str = input() n1 = int(sequence_str.count('1')) n2 = int(sequence_str.count('2')) n3 = int(sequence_str.count('3')) if k >= n1: print(str(n3+min(k,n1+n2))) sys.exit(0) if k < n1 and 2 * t > n3 + k: print(-1) sys.exit(0) #Drive to office possible patterns possible_start_index = [] for x in range(0, n - 2*t): possible = True k_to_use = k for y in range(0,t): if sequence_str[x+y] != '3': k_to_use-=1 if k_to_use >=0: possible_start_index.append(Journey(x,x+t-1,k-k_to_use)) #Drive from office possible patterns possible_end_index = [] for x in range(t, n-(t-1)): possible = True k_to_use = k for y in range(0,t): if sequence_str[x+y] != '3': k_to_use-=1 if k_to_use >=0: possible_end_index.append(Journey(x,x+t-1,k-k_to_use)) if len(possible_start_index) == 0 or len(possible_end_index) == 0: print(-1) sys.exit(0) #Symulacja dla każdej kombinacji max_days = -1 for s in range(0,len(possible_start_index)): for e in range(0,len(possible_end_index)): if possible_start_index[s].used_k + possible_end_index[e].used_k <= k and possible_start_index[s].end < possible_end_index[e].start: current_days = 0 free_k = k - (possible_start_index[s].used_k + possible_end_index[e].used_k) for i in range(0,n): if i < possible_start_index[s].start or i > possible_end_index[e].end: if sequence_str[i] == '3': current_days+=1 elif sequence_str[i] == '1': free_k-=1 if free_k<0: current_days = -1 continue if free_k>0 and current_days>-1: current_days +=free_k if current_days>max_days: max_days = current_days print(max_days) |
English