Skip to content

Instantly share code, notes, and snippets.

@dsapandora
Created September 16, 2020 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsapandora/ed8bc5fde6eb1f5a6afb9da8fdca0dd8 to your computer and use it in GitHub Desktop.
Save dsapandora/ed8bc5fde6eb1f5a6afb9da8fdca0dd8 to your computer and use it in GitHub Desktop.
from math import inf
from bisect import bisect_left as bLeft, bisect_right as bRight
from collections import defaultdict
def getHealth(seq, first, last, largest):
h, ls = 0, len(seq)
for f in range(ls):
for j in range(1, largest+1):
if f+j > ls:
break
sub = seq[f:f+j]
if sub not in subs:
break
if sub not in gMap:
continue
ids, hs = gMap[sub]
h += hs[bRight(ids, last)]-hs[bLeft(ids, first)]
return h
if __name__ == '__main__':
n = int(input())
genes = input().rstrip().split()
health = list(map(int, input().rstrip().split()))
gMap = defaultdict(lambda: [[], [0]])
subs = set()
for i, gene in enumerate(genes):
gMap[gene][0].append(i)
for j in range(1, min(len(gene), 500)+1):
subs.add(gene[:j])
for v in gMap.values():
for i, ix in enumerate(v[0]):
v[1].append(v[1][i]+health[ix])
largest = max(list(map(len, genes)))
hMin, hMax = inf, 0
s = int(input())
for s_itr in range(s):
firstLastd = input().split()
first = int(firstLastd[0])
last = int(firstLastd[1])
d = firstLastd[2]
h = getHealth(d, first, last, largest)
hMin, hMax = min(hMin, h), max(hMax, h)
print(hMin, hMax)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment