#!/usr/bin/env python3
import sys
from collections import defaultdict

sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(N, K, PS, CS):
    PS = [x - 1 for x in PS]
    CS = [CS[PS[i]] for i in range(N)]
    visited = {}
    loops = []
    loopScore = []
    for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

    pos = list(range(N))
    ret = -INF
    for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

    return ret


def main():
    # parse input
    N, K = map(int, input().split())
    PS = list(map(int, input().split()))
    CS = list(map(int, input().split()))
    print(solve(N, K, PS, CS))


# tests
T1 = """
5 2
2 4 5 1 3
3 4 -10 -8 8
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
8
"""

T2 = """
2 3
2 1
10 -7
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
13
"""

T3 = """
3 3
3 1 2
-1000 -2000 -3000
"""
TEST_T3 = """
>>> as_input(T3)
>>> main()
-1000
"""

T4 = """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
"""
TEST_T4 = """
>>> as_input(T4)
>>> main()
29507023469
"""

T5 = """
3 1000
2 3 1
1 0 2
"""
TEST_T5 = """
>>> as_input(T5)
>>> main()
1001
"""

T6 = """
3 1000
2 3 1
1 1 -3
"""
TEST_T6 = """
>>> as_input(T6)
>>> main()
2
"""

T7 = """
4 1000
2 1 4 3
1 1 -10000 10000
"""
TEST_T7 = """
>>> as_input(T7)
>>> main()
10000
"""

T8 = """
4 1000
2 1 4 3
1 1 -10000 10001
"""
TEST_T8 = """
>>> as_input(T8)
>>> main()
10500
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main() import sys sys from collections import defaultdict sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 INF = 10 ** 9 + 1 INF 10 ** 9 + 1 10 ** 9 10 9 1 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 def debug(*x):
    print(*x, file=sys.stderr) debug print(*x, file=sys.stderr) print *x x file=sys.stderr file sys.stderr sys stderr *x def solve(N, K, PS, CS):
    PS = [x - 1 for x in PS]
    CS = [CS[PS[i]] for i in range(N)]
    visited = {}
    loops = []
    loopScore = []
    for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

    pos = list(range(N))
    ret = -INF
    for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

    return ret solve x - 1 for x in PS for x in PS x PS for x in PS x - 1 x 1 PS = [x - 1 for x in PS] PS [x - 1 for x in PS] CS[PS[i]] for i in range(N) for i in range(N) i range(N) range N for i in range(N) CS[PS[i]] CS PS[i] PS i CS = [CS[PS[i]] for i in range(N)] CS [CS[PS[i]] for i in range(N)] visited = {} visited {} loops = [] loops [] loopScore = [] loopScore [] for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

     i range(N) range N loop = [] loop [] c = 0 c 0 while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
         i not in visited i visited visited[i] = True visited[i] visited i True c += CS[i] c CS[i] CS i i = PS[i] i PS[i] PS i loop.append(i) loop.append loop append i if loop:
            loops.append(loop)
            loopScore.append(c)

     loop loops.append(loop) loops.append loops append loop loopScore.append(c) loopScore.append loopScore append c pos = list(range(N)) pos list(range(N)) list range(N) range N ret = -INF ret -INF INF for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

     i loop enumerate(loops) enumerate loops if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

     loopScore[i] > 0 loopScore[i] loopScore i 0 baseScore = loopScore[i] * (K // len(loop)) baseScore loopScore[i] * (K // len(loop)) loopScore[i] loopScore i K // len(loop) K len(loop) len loop r = K % len(loop) r K % len(loop) K len(loop) len loop if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
             r == 0 r 0 r = len(loop) r len(loop) len loop baseScore -= loopScore[i] baseScore loopScore[i] loopScore i maxscore = 0 maxscore 0 scores = defaultdict(int) scores defaultdict(int) defaultdict int for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
             i range(r) range r for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                 x loop scores[x] += CS[pos[x]] scores[x] scores x CS[pos[x]] CS pos[x] pos x pos[x] = PS[pos[x]] pos[x] pos x PS[pos[x]] PS pos[x] pos x maxscore = max(maxscore, max(scores.values())) maxscore max(maxscore, max(scores.values())) max maxscore max(scores.values()) max scores.values() scores.values scores values ret = max(maxscore + baseScore, ret) ret max(maxscore + baseScore, ret) max maxscore + baseScore maxscore baseScore ret r = len(loop) r len(loop) len loop maxscore = -INF maxscore -INF INF scores = defaultdict(int) scores defaultdict(int) defaultdict int for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
             i range(r) range r for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                 x loop scores[x] += CS[pos[x]] scores[x] scores x CS[pos[x]] CS pos[x] pos x pos[x] = PS[pos[x]] pos[x] pos x PS[pos[x]] PS pos[x] pos x maxscore = max(maxscore, max(scores.values())) maxscore max(maxscore, max(scores.values())) max maxscore max(scores.values()) max scores.values() scores.values scores values ret = max(maxscore, ret) ret max(maxscore, ret) max maxscore ret return ret ret N N K K PS PS CS CS def main():
    # parse input
    N, K = map(int, input().split())
    PS = list(map(int, input().split()))
    CS = list(map(int, input().split()))
    print(solve(N, K, PS, CS))


# tests main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K PS = list(map(int, input().split())) PS list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split CS = list(map(int, input().split())) CS list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split print(solve(N, K, PS, CS)) print solve(N, K, PS, CS) solve N K PS CS T1 = """
5 2
2 4 5 1 3
3 4 -10 -8 8
""" T1 """
5 2
2 4 5 1 3
3 4 -10 -8 8
""" TEST_T1 = """
>>> as_input(T1)
>>> main()
8
""" TEST_T1 """
>>> as_input(T1)
>>> main()
8
""" T2 = """
2 3
2 1
10 -7
""" T2 """
2 3
2 1
10 -7
""" TEST_T2 = """
>>> as_input(T2)
>>> main()
13
""" TEST_T2 """
>>> as_input(T2)
>>> main()
13
""" T3 = """
3 3
3 1 2
-1000 -2000 -3000
""" T3 """
3 3
3 1 2
-1000 -2000 -3000
""" TEST_T3 = """
>>> as_input(T3)
>>> main()
-1000
""" TEST_T3 """
>>> as_input(T3)
>>> main()
-1000
""" T4 = """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
""" T4 """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
""" TEST_T4 = """
>>> as_input(T4)
>>> main()
29507023469
""" TEST_T4 """
>>> as_input(T4)
>>> main()
29507023469
""" T5 = """
3 1000
2 3 1
1 0 2
""" T5 """
3 1000
2 3 1
1 0 2
""" TEST_T5 = """
>>> as_input(T5)
>>> main()
1001
""" TEST_T5 """
>>> as_input(T5)
>>> main()
1001
""" T6 = """
3 1000
2 3 1
1 1 -3
""" T6 """
3 1000
2 3 1
1 1 -3
""" TEST_T6 = """
>>> as_input(T6)
>>> main()
2
""" TEST_T6 """
>>> as_input(T6)
>>> main()
2
""" T7 = """
4 1000
2 1 4 3
1 1 -10000 10000
""" T7 """
4 1000
2 1 4 3
1 1 -10000 10000
""" TEST_T7 = """
>>> as_input(T7)
>>> main()
10000
""" TEST_T7 """
>>> as_input(T7)
>>> main()
10000
""" T8 = """
4 1000
2 1 4 3
1 1 -10000 10001
""" T8 """
4 1000
2 1 4 3
1 1 -10000 10001
""" TEST_T8 = """
>>> as_input(T8)
>>> main()
10500
""" TEST_T8 """
>>> as_input(T8)
>>> main()
10500
""" def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k) _test import doctest doctest doctest.testmod() doctest.testmod doctest testmod g = globals() g globals() globals for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k) k sorted(g) sorted g if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k) k.startswith("TEST_") k.startswith k startswith "TEST_" doctest.run_docstring_examples(g[k], g, name=k) doctest.run_docstring_examples doctest run_docstring_examples g[k] g k g name=k name k def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii") as_input "use in test, use given string as input file" import io io f = io.StringIO(s.strip()) f io.StringIO(s.strip()) io.StringIO io StringIO s.strip() s.strip s strip g = globals() g globals() globals g["input"] = lambda: bytes(f.readline(), "ascii") g["input"] g "input" lambda: bytes(f.readline(), "ascii") bytes(f.readline(), "ascii") bytes f.readline() f.readline f readline "ascii" g["read"] = lambda: bytes(f.read(), "ascii") g["read"] g "read" lambda: bytes(f.read(), "ascii") bytes(f.read(), "ascii") bytes f.read() f.read f read "ascii" s s input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit() sys.argv[-1] == "-t" sys.argv[-1] sys.argv sys argv -1 "-t" print("testing") print "testing" _test() _test sys.exit() sys.exit sys exit main() main INF = 10 ** 9 + 1 10 ** 9 + 1 INF def main():
    # parse input
    N, K = map(int, input().split())
    PS = list(map(int, input().split()))
    CS = list(map(int, input().split()))
    print(solve(N, K, PS, CS))


# tests def main():
    # parse input
    N, K = map(int, input().split())
    PS = list(map(int, input().split()))
    CS = list(map(int, input().split()))
    print(solve(N, K, PS, CS))


# tests main input = sys.stdin.buffer.readline sys.stdin.buffer.readline input TEST_T5 = """
>>> as_input(T5)
>>> main()
1001
""" """
>>> as_input(T5)
>>> main()
1001
""" TEST_T5 def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k) def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k) _test TEST_T6 = """
>>> as_input(T6)
>>> main()
2
""" """
>>> as_input(T6)
>>> main()
2
""" TEST_T6 T4 = """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
""" """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
""" T4 T1 = """
5 2
2 4 5 1 3
3 4 -10 -8 8
""" """
5 2
2 4 5 1 3
3 4 -10 -8 8
""" T1 TEST_T2 = """
>>> as_input(T2)
>>> main()
13
""" """
>>> as_input(T2)
>>> main()
13
""" TEST_T2 read = sys.stdin.buffer.read sys.stdin.buffer.read read def debug(*x):
    print(*x, file=sys.stderr) def debug(*x):
    print(*x, file=sys.stderr) debug TEST_T1 = """
>>> as_input(T1)
>>> main()
8
""" """
>>> as_input(T1)
>>> main()
8
""" TEST_T1 T3 = """
3 3
3 1 2
-1000 -2000 -3000
""" """
3 3
3 1 2
-1000 -2000 -3000
""" T3 TEST_T7 = """
>>> as_input(T7)
>>> main()
10000
""" """
>>> as_input(T7)
>>> main()
10000
""" TEST_T7 T6 = """
3 1000
2 3 1
1 1 -3
""" """
3 1000
2 3 1
1 1 -3
""" T6 T7 = """
4 1000
2 1 4 3
1 1 -10000 10000
""" """
4 1000
2 1 4 3
1 1 -10000 10000
""" T7 def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii") def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii") as_input TEST_T3 = """
>>> as_input(T3)
>>> main()
-1000
""" """
>>> as_input(T3)
>>> main()
-1000
""" TEST_T3 MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD TEST_T4 = """
>>> as_input(T4)
>>> main()
29507023469
""" """
>>> as_input(T4)
>>> main()
29507023469
""" TEST_T4 TEST_T8 = """
>>> as_input(T8)
>>> main()
10500
""" """
>>> as_input(T8)
>>> main()
10500
""" TEST_T8 T5 = """
3 1000
2 3 1
1 0 2
""" """
3 1000
2 3 1
1 0 2
""" T5 def solve(N, K, PS, CS):
    PS = [x - 1 for x in PS]
    CS = [CS[PS[i]] for i in range(N)]
    visited = {}
    loops = []
    loopScore = []
    for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

    pos = list(range(N))
    ret = -INF
    for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

    return ret def solve(N, K, PS, CS):
    PS = [x - 1 for x in PS]
    CS = [CS[PS[i]] for i in range(N)]
    visited = {}
    loops = []
    loopScore = []
    for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

    pos = list(range(N))
    ret = -INF
    for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

    return ret solve T8 = """
4 1000
2 1 4 3
1 1 -10000 10001
""" """
4 1000
2 1 4 3
1 1 -10000 10001
""" T8 T2 = """
2 3
2 1
10 -7
""" """
2 3
2 1
10 -7
""" T2
#!/usr/bin/env python3
import sys
from collections import defaultdict

sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(N, K, PS, CS):
    PS = [x - 1 for x in PS]
    CS = [CS[PS[i]] for i in range(N)]
    visited = {}
    loops = []
    loopScore = []
    for i in range(N):
        loop = []
        c = 0
        while i not in visited:
            visited[i] = True
            c += CS[i]
            i = PS[i]
            loop.append(i)
        if loop:
            loops.append(loop)
            loopScore.append(c)

    pos = list(range(N))
    ret = -INF
    for i, loop in enumerate(loops):
        if loopScore[i] > 0:
            baseScore = loopScore[i] * (K // len(loop))
            r = K % len(loop)
            if r == 0:
                r = len(loop)
                baseScore -= loopScore[i]
            maxscore = 0
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore + baseScore, ret)
        else:
            r = len(loop)
            maxscore = -INF
            scores = defaultdict(int)
            for i in range(r):
                for x in loop:
                    scores[x] += CS[pos[x]]
                    pos[x] = PS[pos[x]]
                maxscore = max(maxscore, max(scores.values()))
            ret = max(maxscore, ret)

    return ret


def main():
    # parse input
    N, K = map(int, input().split())
    PS = list(map(int, input().split()))
    CS = list(map(int, input().split()))
    print(solve(N, K, PS, CS))


# tests
T1 = """
5 2
2 4 5 1 3
3 4 -10 -8 8
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
8
"""

T2 = """
2 3
2 1
10 -7
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
13
"""

T3 = """
3 3
3 1 2
-1000 -2000 -3000
"""
TEST_T3 = """
>>> as_input(T3)
>>> main()
-1000
"""

T4 = """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
"""
TEST_T4 = """
>>> as_input(T4)
>>> main()
29507023469
"""

T5 = """
3 1000
2 3 1
1 0 2
"""
TEST_T5 = """
>>> as_input(T5)
>>> main()
1001
"""

T6 = """
3 1000
2 3 1
1 1 -3
"""
TEST_T6 = """
>>> as_input(T6)
>>> main()
2
"""

T7 = """
4 1000
2 1 4 3
1 1 -10000 10000
"""
TEST_T7 = """
>>> as_input(T7)
>>> main()
10000
"""

T8 = """
4 1000
2 1 4 3
1 1 -10000 10001
"""
TEST_T8 = """
>>> as_input(T8)
>>> main()
10500
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()
