Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a = int(input())
- qw = [int(i) for i in input().split()]
- p = input()
- now = [[0,p],[qw[0],p[::-1]]]
- for u in range(1,a):
- inp = input()
- er1 = [[],[]]
- for i in now:
- if sorted([i[1],inp]) == [i[1],inp]:
- er1[0].append(i[0])
- if sorted([i[1],inp[::-1]]) == [i[1],inp[::-1]]:
- er1[1].append(i[0]+qw[u])
- now = []
- if len(er1[0]) != 0:
- now.append([min(er1[0]),inp])
- if len(er1[1]) != 0:
- now.append([min(er1[1]),inp[::-1]])
- if len(er1[1]) == 0 and len(er1[0]) == 0:
- print(-1)
- exit()
- minn = 10**15
- for i in now:
- minn = min(minn,i[0])
- print(minn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement