from collections import * from itertools import * from functools import * from heapq import * import sys import math input = sys.stdin.readline def segfunc(x, y): return min(x,y) ide_ele = (2,-1) class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc, ide_ele): """ 初期化 init_val: 配列の初期値 """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res S = list(input())[:-1] D = [list(map(int,input().split())) for _ in range(26)] S = [ord(i)-ord('a') for i in S] N = len(S) INF = N+100 dist = defaultdict(lambda:INF) U = [[(1-int(S[i]==j),i) for i in range(N)] for j in range(26)] T = [SegTree(U[i],segfunc,ide_ele) for i in range(26)] for i in range(N-1): s = S[i] for j in range(26): _,k = T[j].query(i+1,N) if _==1: continue dist[(s,j)] = min(dist[(s,j)],k-i) X = [['Y']*26 for _ in range(26)] for i in range(26): for j in range(26): if dist[(i,j)]