import numpy as np


def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc


def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst]


def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise


def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result)


if __name__ == "__main__":
    main() import numpy as np numpy def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc parse n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = list(map(int, input().split())) p list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c)) for i, (pi, ci) in enumerate(zip(p, c)) i (pi, ci) pi ci enumerate(zip(p, c)) enumerate zip(p, c) zip p c for i, (pi, ci) in enumerate(zip(p, c)) i (pi - 1, ci) pi - 1 pi 1 ci pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))} pc {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))} return k, pc return k, pc k pc def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] split_simple k, pc = parse() k parse() parse pc lst = [[]] lst [[]] [] lst[0].append(pc.pop(0)) [0].append [0] lst 0 append pc.pop(0) pc.pop pc pop 0 while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
     True if not pc:
            break
         not pc pc break p, _ = lst[-1][-1] p lst[-1][-1] [-1] lst -1 -1 _ if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
     p in pc p pc pcj = pc.pop(p) pcj pc.pop(p) pc.pop pc pop p lst[-1].append(pcj) [-1].append [-1] lst -1 append pcj j = next(iter(pc.keys())) j next(iter(pc.keys())) next iter(pc.keys()) iter pc.keys() pc.keys pc keys pcj = pc.pop(j) pcj pc.pop(j) pc.pop pc pop j lst.append([pcj]) lst.append lst append [pcj] pcj np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst for lst_ in lst lst_ lst for lst_ in lst np.array([ls[1] for ls in lst_], dtype=np.long) np.array np array [ls[1] for ls in lst_] dtype=np.long dtype np.long np long return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] k [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise inner if sim.shape[1] == 1:
        return sim.max()
     sim.shape[1] == 1 sim.shape[1] sim.shape sim shape 1 1 return sim.max() sim.max() sim.max sim max if k < n or s <= 0:
        return sim.max()
     k < n or s <= 0 k < n k n s <= 0 s 0 return sim.max() sim.max() sim.max sim max div = k // n div k // n k n mod = k % n mod k % n k n if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
     mod == 0 mod 0 return max(s * div, s * (div - 1) + sim.max()) max(s * div, s * (div - 1) + sim.max()) max s * div s div s * (div - 1) + sim.max() s * (div - 1) s div - 1 div 1 sim.max() sim.max sim max try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise print(mod) print mod print(sim) print sim print(sim.shape) print sim.shape sim shape raise return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max()) max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max()) max s * div + sim[:, :mod].max() s * div s div sim[:, :mod].max() [:, :mod].max [:, :mod] sim : max s * (div - 1) + sim[:, :mod].max() s * (div - 1) s div - 1 div 1 sim[:, :mod].max() [:, :mod].max [:, :mod] sim : max n n k k s s sim sim def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result) main k, data_set = split_simple() k split_simple() split_simple data_set d.min() for d in data_set for d in data_set d data_set for d in data_set d.min() d.min d min result = min([d.min() for d in data_set]) result min([d.min() for d in data_set]) min [d.min() for d in data_set] for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
     data data_set n = data.size n data.size data size rep = np.repeat(data[None], 2, axis=0).flatten() rep np.repeat(data[None], 2, axis=0).flatten() np.repeat(data[None], 2, axis=0).flatten np.repeat(data[None], 2, axis=0) np.repeat np repeat data[None] data None 2 axis=0 axis 0 flatten np.cumsum(rep[i:i+min(k, n)]) for i in range(n) for i in range(n) i range(n) range n for i in range(n) np.cumsum(rep[i:i+min(k, n)]) np.cumsum np cumsum rep[i:i+min(k, n)] rep i:i+min(k, n) i i+min(k, n) i min(k, n) min k n sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)]) sim np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)]) np.array np array [np.cumsum(rep[i:i+min(k, n)]) for i in range(n)] cand = inner(n, k, data.sum(), sim) cand inner(n, k, data.sum(), sim) inner n k data.sum() data.sum data sum sim result = max(result, cand) result max(result, cand) max result cand print(result) print result if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst] split_simple def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result) def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result) main def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise inner def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc parse
import numpy as np


def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc


def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst]


def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise


def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result)


if __name__ == "__main__":
    main()
