Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. a = int(input())
  2. qw = [int(i) for i in input().split()]
  3. p = input()
  4. now = [[0,p],[qw[0],p[::-1]]]
  5. for u in range(1,a):
  6. inp = input()
  7. er1 = [[],[]]
  8. for i in now:
  9. if sorted([i[1],inp]) == [i[1],inp]:
  10. er1[0].append(i[0])
  11. if sorted([i[1],inp[::-1]]) == [i[1],inp[::-1]]:
  12. er1[1].append(i[0]+qw[u])
  13. now = []
  14. if len(er1[0]) != 0:
  15. now.append([min(er1[0]),inp])
  16. if len(er1[1]) != 0:
  17. now.append([min(er1[1]),inp[::-1]])
  18. if len(er1[1]) == 0 and len(er1[0]) == 0:
  19. print(-1)
  20. exit()
  21. minn = 10**15
  22. for i in now:
  23. minn = min(minn,i[0])
  24. print(minn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement