@lru_cache(maxsize=None)
def f(s=tuple((0 for _ in range(9)))):
    if all(s):
        res = 0
        for i in range(6):
            res += ((s[i] == s[(i + 3)]) * b[i])
        for i in range(8):
            res += ((s[i] == s[(i + 1)]) * c[i])
        return res
    cand = [i for i in range(9) if (not s[i])]
    flg = (len(cand) & 1)
    s = list(s)
    res = []
    for i in cand:
        s[i] = ((flg ^ 1) + 1)
        res.append(f(tuple(s)))
        s[i] = 0
    return sorted(res, reverse=flg)[0]
@lru_cache(maxsize=None)
def f(s=tuple((0 for _ in range(9)))):
    if all(s):
        res = 0
        for i in range(6):
            res += ((s[i] == s[(i + 3)]) * b[i])
        for i in range(8):
            res += ((s[i] == s[(i + 1)]) * c[i])
        return res
    cand = [i for i in range(9) if (not s[i])]
    flg = (len(cand) & 1)
    s = list(s)
    res = []
    for i in cand:
        s[i] = ((flg ^ 1) + 1)
        res.append(f(tuple(s)))
        s[i] = 0
    return sorted(res, reverse=flg)[0]