結果

問題 No.3653 Space-Time Courier
コンテスト
ユーザー kemuniku
提出日時 2026-08-31 23:16:35
言語 Nim
(2.2.10 + ACL)
コンパイル:
nim --nimcache=~ --hints:off -o:a.out -d:release cpp _filename_
実行:
./a.out
結果
AC  
実行時間 3,479 ms / 4,000 ms
+ 548µs
コード長 30,478 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,265 ms
コンパイル使用メモリ 154,468 KB
実行使用メモリ 54,400 KB
最終ジャッジ日時 2026-08-31 23:17:32
合計ジャッジ時間 43,194 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2])
# source: src/cplib/tmpl/sheep.nim
ImportExpand "cplib/tmpl/sheep" <=== "when not declared CPLIB_TMPL_SHEEP:\n    const CPLIB_TMPL_SHEEP* = 1\n    {.warning[UnusedImport]: off.}\n    {.hint[XDeclaredButNotUsed]: off.}\n    import algorithm\n    import sequtils\n    import tables\n    import macros\n    import math\n    import sets\n    import strutils\n    import strformat\n    import sugar\n    import heapqueue\n    import streams\n    import deques\n    import bitops\n    import std/lenientops\n    import options\n    #入力系\n    proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n    proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n    proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n    proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\n    proc si(): string {.inline.} =\n        result = \"\"\n        var c: char\n        while true:\n            c = getchar()\n            if c == ' ' or c == '\\n' or c == '\\255':\n                break\n            result &= c\n    \n    # 出力系\n    # 1. 実際の処理を行う proc (openArray を受け取る)\n    proc print_internal(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: openArray[string]) =\n        for i in 0 ..< args.len:\n            prop.f.write(args[i])\n            if i != args.len - 1:\n                prop.f.write(prop.sepc)\n            else:\n                prop.f.write(prop.endc)\n        if prop.flush:\n            prop.f.flushFile()\n\n    # 2. ユーザーが呼び出すためのインターフェース (varargs を受け取る)\n    proc print*(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: varargs[string, `$`]) =\n        # varargs は内部では openArray として扱えるので、そのまま渡せる\n        print_internal(prop, args)\n\n    proc print*(args: varargs[string, `$`]) =\n        # こちらも内部用の proc を呼ぶ\n        print_internal((f: stdout, sepc: \" \", endc: \"\\n\", flush: false), args)\n    macro getSymbolName(x: typed): string = x.toStrLit\n    macro debug*(args: varargs[untyped]): untyped =\n        when defined(debug):\n            result = newNimNode(nnkStmtList, args)\n            template prop(e: string = \"\"): untyped = (f: stderr, sepc: \"\", endc: e, flush: true)\n            for i, arg in args:\n                if arg.kind == nnkStrLit:\n                    result.add(quote do: print(prop(), \"\\\"\", `arg`, \"\\\"\"))\n                else:\n                    result.add(quote do: print(prop(\": \"), getSymbolName(`arg`)))\n                    result.add(quote do: print(prop(), `arg`))\n                if i != args.len - 1: result.add(quote do: print(prop(), \", \"))\n                else: result.add(quote do: print(prop(), \"\\n\"))\n        else:\n            return (quote do: discard)\n    #chmin,chmax\n    template `max=`(x, y) =\n        let yVal = y # yが計算式の場合に評価を1回にするため\n        if x < yVal:\n            x = yVal\n\n    template `min=`(x, y) =\n        let yVal = y\n        if x > yVal:\n            x = yVal\n    proc chmin[T](x: var T, y: T):bool=\n        if x > y:\n            x = y\n            return true\n        return false\n    proc chmax[T](x: var T, y: T):bool=\n        if x < y:\n            x = y\n            return true\n        return false\n    #bit演算\n    proc `%`*(x: int, y: int): int =\n        result = x mod y\n        if y > 0 and result < 0: result += y\n        if y < 0 and result > 0: result += y\n    proc `//`*(x: int, y: int): int{.inline.} =\n        result = x div y\n        if y > 0 and result * y > x: result -= 1\n        if y < 0 and result * y < x: result -= 1\n    proc `%=`(x: var int, y: int): void = x = x%y\n    proc `//=`(x: var int, y: int): void = x = x//y\n    proc `**`(x: int, y: int): int = x^y\n    proc `**=`(x: var int, y: int): void = x = x^y\n    proc `^`(x: int, y: int): int = x xor y\n    proc `|`(x: int, y: int): int = x or y\n    proc `&`(x: int, y: int): int = x and y\n    proc `>>`(x: int, y: int): int = x shr y\n    proc `<<`(x: int, y: int): int = x shl y\n    proc `~`(x: int): int = not x\n    proc `^=`(x: var int, y: int): void = x = x ^ y\n    proc `&=`(x: var int, y: int): void = x = x & y\n    proc `|=`(x: var int, y: int): void = x = x | y\n    proc `>>=`(x: var int, y: int): void = x = x >> y\n    proc `<<=`(x: var int, y: int): void = x = x << y\n    proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n    #便利な変換\n    proc `!`(x: char, a = '0'): int = int(x)-int(a)\n    #定数\n    when not declared CPLIB_UTILS_CONSTANTS:\n        const CPLIB_UTILS_CONSTANTS* = 1\n        const INF32*: int32 = 1001000027.int32\n        const INF64*: int = int(3300300300300300491)\n    \n    const INF = INF64\n    #converter\n\n    #range\n    iterator range(start: int, ends: int, step: int): int =\n        var i = start\n        if step < 0:\n            while i > ends:\n                yield i\n                i += step\n        elif step > 0:\n            while i < ends:\n                yield i\n                i += step\n    iterator range(ends: int): int = (for i in 0..<ends: yield i)\n    iterator range(start: int, ends: int): int = (for i in\n            start..<ends: yield i)\n\n    #joinが非stringでめちゃくちゃ遅いやつのパッチ\n    proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n\n    proc dump[T](arr:seq[seq[T]])=\n        for i in 0..<len(arr):\n            echo arr[i]\n\n    proc sum(slice:HSlice[int,int]):int=\n        return (slice.a+slice.b)*len(slice)//2\n    \n    proc `<`[T](l,r:seq[T]):bool=\n        for i in 0..<min(len(l),len(r)):\n            if l[i] > r[i]:\n                return false\n            elif l[i] < r[i]:\n                return true\n        return len(l) < len(r)\n    \n    # Yes/No\n    proc yes*(b: bool = true): void = print(if b: \"Yes\" else: \"No\")\n    proc no*(b: bool = true): void = yes(not b)\n\n    proc takahashi(b:bool = true) : void = print(if b: \"Takahashi\" else: \"Aoki\")\n    proc aoki(b:bool = true) : void = takahashi(not b)\n\n    template dblock(body: untyped) =\n        when defined(debug):\n            block:\n                body\n"
# source: src/cplib/graph/graph.nim
ImportExpand "cplib/graph/graph" <=== "when not declared CPLIB_GRAPH_GRAPH:\n    const CPLIB_GRAPH_GRAPH* = 1\n\n    import sequtils\n    import math\n    type DynamicGraph*[T] = ref object of RootObj\n        edges*: seq[seq[(int32, T)]]\n        len*: int\n    type StaticGraph*[T] = ref object of RootObj\n        src*, dst*: seq[int32]\n        cost*: seq[T]\n        elist*: seq[(int32, T)]\n        start*: seq[int32]\n        len*: int\n\n    type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n    type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n    type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n    type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n    type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n    type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n    type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n    type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n\n    type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n    type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n    type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n    type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n    type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n    type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n    type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n\n    proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n        g.edges[u].add((v.int32, cost))\n        if not directed: g.edges[v].add((u.int32, cost))\n\n    proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n        result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n    proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n        g.add_edge_dynamic_impl(u, v, cost, true)\n\n    proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n        result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n    proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n        g.add_edge_dynamic_impl(u, v, cost, false)\n\n    proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n        result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n    proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n        g.add_edge_dynamic_impl(u, v, 1, true)\n\n    proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n        result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n    proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n        g.add_edge_dynamic_impl(u, v, 1, false)\n\n    proc len*[T](G: WeightedGraph[T]): int = G.len\n    proc len*(G: UnWeightedGraph): int = G.len\n\n    iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n        for e in g.edges[x]: yield (e[0].int, e[1])\n    iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n        for e in g.edges[x]: yield e[0].int\n\n    proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n        g.src.add(u.int32)\n        g.dst.add(v.int32)\n        g.cost.add(cost)\n        if not directed:\n            g.src.add(v.int32)\n            g.dst.add(u.int32)\n            g.cost.add(cost)\n\n    proc build_impl*[T](g: StaticGraph[T]) =\n        g.start = newSeqWith(g.len + 1, 0.int32)\n        for i in 0..<g.src.len:\n            g.start[g.src[i]] += 1\n        g.start.cumsum\n        g.elist = newSeq[(int32, T)](g.start[^1])\n        for i in countdown(g.src.len - 1, 0):\n            var u = g.src[i]\n            var v = g.dst[i]\n            g.start[u] -= 1\n            g.elist[g.start[u]] = (v, g.cost[i])\n    proc build*(g: StaticGraphTypes) = g.build_impl()\n\n    proc initWeightedDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedDirectedStaticGraph[edgetype] =\n        result = WeightedDirectedStaticGraph[edgetype](\n            src: newSeqOfCap[int32](capacity),\n            dst: newSeqOfCap[int32](capacity),\n            cost: newSeqOfCap[edgetype](capacity),\n            elist: newSeq[(int32, edgetype)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*[T](g: var WeightedDirectedStaticGraph[T], u, v: int, cost: T) =\n        g.add_edge_static_impl(u, v, cost, true)\n\n    proc initWeightedUnDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedUnDirectedStaticGraph[edgetype] =\n        result = WeightedUnDirectedStaticGraph[edgetype](\n            src: newSeqOfCap[int32](capacity*2),\n            dst: newSeqOfCap[int32](capacity*2),\n            cost: newSeqOfCap[edgetype](capacity*2),\n            elist: newSeq[(int32, edgetype)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*[T](g: var WeightedUnDirectedStaticGraph[T], u, v: int, cost: T) =\n        g.add_edge_static_impl(u, v, cost, false)\n\n    proc initUnWeightedDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedDirectedStaticGraph =\n        result = UnWeightedDirectedStaticGraph(\n            src: newSeqOfCap[int32](capacity),\n            dst: newSeqOfCap[int32](capacity),\n            cost: newSeqOfCap[int](capacity),\n            elist: newSeq[(int32, int)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*(g: var UnWeightedDirectedStaticGraph, u, v: int) =\n        g.add_edge_static_impl(u, v, 1, true)\n\n    proc initUnWeightedUnDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedUnDirectedStaticGraph =\n        result = UnWeightedUnDirectedStaticGraph(\n            src: newSeqOfCap[int32](capacity*2),\n            dst: newSeqOfCap[int32](capacity*2),\n            cost: newSeqOfCap[int](capacity*2),\n            elist: newSeq[(int32, int)](0),\n            start: newSeq[int32](0),\n            len: N\n        )\n    proc add_edge*(g: var UnWeightedUnDirectedStaticGraph, u, v: int) =\n        g.add_edge_static_impl(u, v, 1, false)\n\n    proc static_graph_initialized_check*[T](g: StaticGraph[T]) = assert g.start.len > 0, \"Static Graph must be initialized before use.\"\n\n    iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n    iterator `[]`*(g: UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph, x: int): int =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield g.elist[i][0].int\n\n    iterator to_and_cost*[T](g: DynamicGraph[T], x: int): (int, T) =\n        for e in g.edges[x]: yield (e[0].int, e[1])\n    iterator to_and_cost*[T](g: StaticGraph[T], x: int): (int, T) =\n        g.static_graph_initialized_check()\n        for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n    \n    import tables\n\n    type UnWeightedUnDirectedTableGraph*[T] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : UnWeightedUnDirectedGraph\n\n    type UnWeightedDirectedTableGraph*[T] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : UnWeightedDirectedGraph\n\n    type WeightedUnDirectedTableGraph*[T,S] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : WeightedUnDirectedGraph[S]\n\n    type WeightedDirectedTableGraph*[T,S] = object \n        toi* : Table[T,int]\n        v* : seq[T]\n        graph* : WeightedDirectedGraph[S]\n\n    type UnWeightedTableGraph*[T] = UnWeightedUnDirectedTableGraph[T] or UnWeightedDirectedTableGraph[T]\n    type WeightedTableGraph*[T,S] = WeightedUnDirectedTableGraph[T,S] or WeightedDirectedTableGraph[T,S]\n\n    proc initUnWeightedUnDirectedTableGraph*[T](V:openArray[T]):UnWeightedUnDirectedTableGraph[T]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initUnWeightedUnDirectedGraph(len(V))\n        result.v = @V\n\n    proc initUnWeightedDirectedTableGraph*[T](V:openArray[T]):UnWeightedDirectedTableGraph[T]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initUnWeightedDirectedGraph(len(V))\n        result.v = @V\n\n    proc initWeightedUnDirectedTableGraph*[T](V:openArray[T],S:typedesc = int):WeightedUnDirectedTableGraph[T,S]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initWeightedUnDirectedGraph(len(V),S)\n        result.v = @V\n\n    proc initWeightedDirectedTableGraph*[T](V:openArray[T],S:typedesc = int):WeightedDirectedTableGraph[T,S]=\n        for i in 0..<len(V):\n            result.toi[V[i]] = i\n        result.graph = initWeightedDirectedGraph(len(V),S)\n        result.v = @V\n\n    proc add_edge*[T](g: var UnWeightedTableGraph[T],u,v:T)=\n        g.graph.add_edge(g.toi[u],g.toi[v])\n\n    proc add_edge*[T,S](g: var WeightedTableGraph[T,S],u,v:T,cost:S)=\n        g.graph.add_edge(g.toi[u],g.toi[v],cost)\n\n    iterator `[]`*[T,S](g: WeightedDirectedTableGraph[T,S] or WeightedUnDirectedTableGraph[T,S], x: T): (T, S) = \n        for (x,y) in g.graph[g.toi[x]]:\n            yield (g.v[x],y)\n    iterator `[]`*[T](g: UnWeightedDirectedTableGraph[T] or UnWeightedUnDirectedTableGraph[T], x: T): T = \n        for x in g.graph[g.toi[x]]:\n            yield g.v[x]\n"
# source: src/cplib/graph/warshall_floyd.nim
ImportExpand "cplib/graph/warshall_floyd" <=== "when not declared CPLIB_GRAPH_WARSHALLFLOYD:\n    const CPLIB_GRAPH_WARSHALLFLOYD* = 1\n    import sequtils\n\n    when defined(cpp) and sizeof(int) == 8:\n        {.emit: \"\"\"\n        #ifndef CPLIB_GRAPH_WARSHALL_FLOYD_AVX2_HPP\n        #define CPLIB_GRAPH_WARSHALL_FLOYD_AVX2_HPP\n\n        #include <immintrin.h>\n\n        #include <cstddef>\n        #include <cstdint>\n\n        #ifndef CPLIB_WARSHALL_FLOYD_BLOCK_SIZE\n        #define CPLIB_WARSHALL_FLOYD_BLOCK_SIZE 192\n        #endif\n\n        #pragma GCC push_options\n        #pragma GCC target(\"avx2\")\n        #pragma GCC optimize(\"O3\")\n\n        static inline void cplib_warshall_floyd_relax_avx2(\n                std::int64_t* row_i,\n                const std::int64_t* row_k,\n                std::int64_t dik,\n                std::size_t begin,\n                std::size_t end,\n                __m256i inf4,\n                std::int64_t inf) {\n            const __m256i dik4 = _mm256_set1_epi64x(dik);\n            std::size_t j = begin;\n            for (; j + 4 <= end; j += 4) {\n                const __m256i dkj = _mm256_loadu_si256(\n                    reinterpret_cast<const __m256i*>(row_k + j));\n                const __m256i dij = _mm256_loadu_si256(\n                    reinterpret_cast<const __m256i*>(row_i + j));\n                const __m256i candidate = _mm256_add_epi64(dik4, dkj);\n                const __m256i unreachable = _mm256_cmpeq_epi64(dkj, inf4);\n                const __m256i improves = _mm256_cmpgt_epi64(dij, candidate);\n                const __m256i take = _mm256_andnot_si256(unreachable, improves);\n                const __m256i updated = _mm256_blendv_epi8(\n                    dij, candidate, take);\n                _mm256_storeu_si256(\n                    reinterpret_cast<__m256i*>(row_i + j), updated);\n            }\n            for (; j < end; ++j) {\n                if (row_k[j] != inf) {\n                    const std::int64_t candidate = dik + row_k[j];\n                    if (candidate < row_i[j]) row_i[j] = candidate;\n                }\n            }\n        }\n\n        extern \"C\" bool cplib_warshall_floyd_int64_avx2(\n                void* raw_rows,\n                std::size_t n,\n                std::int64_t zero,\n                std::int64_t inf) {\n            std::int64_t** d = static_cast<std::int64_t**>(raw_rows);\n            const __m256i inf4 = _mm256_set1_epi64x(inf);\n            constexpr std::size_t block_size =\n                CPLIB_WARSHALL_FLOYD_BLOCK_SIZE;\n\n            for (std::size_t i = 0; i < n; ++i) {\n                if (d[i][i] < zero) return true;\n            }\n\n            for (std::size_t kk = 0; kk < n; kk += block_size) {\n                const std::size_t kend =\n                    kk + block_size < n ? kk + block_size : n;\n\n                // Phase 1: close the diagonal block.\n                for (std::size_t k = kk; k < kend; ++k) {\n                    const std::int64_t* const row_k = d[k];\n                    for (std::size_t i = kk; i < kend; ++i) {\n                        const std::int64_t dik = d[i][k];\n                        if (dik != inf) cplib_warshall_floyd_relax_avx2(\n                            d[i], row_k, dik, kk, kend, inf4, inf);\n                    }\n                }\n\n                for (std::size_t i = 0; i < n; ++i) {\n                    if (d[i][i] < zero) return true;\n                }\n\n                // Phase 2a: update the blocks in the diagonal block row.\n                for (std::size_t jj = 0; jj < n; jj += block_size) {\n                    if (jj == kk) continue;\n                    const std::size_t jend =\n                        jj + block_size < n ? jj + block_size : n;\n                    for (std::size_t k = kk; k < kend; ++k) {\n                        const std::int64_t* const row_k = d[k];\n                        for (std::size_t i = kk; i < kend; ++i) {\n                            const std::int64_t dik = d[i][k];\n                            if (dik != inf) cplib_warshall_floyd_relax_avx2(\n                                d[i], row_k, dik, jj, jend, inf4, inf);\n                        }\n                    }\n                }\n\n                // Phase 2b: update the blocks in the diagonal block column.\n                for (std::size_t ii = 0; ii < n; ii += block_size) {\n                    if (ii == kk) continue;\n                    const std::size_t iend =\n                        ii + block_size < n ? ii + block_size : n;\n                    for (std::size_t k = kk; k < kend; ++k) {\n                        const std::int64_t* const row_k = d[k];\n                        for (std::size_t i = ii; i < iend; ++i) {\n                            const std::int64_t dik = d[i][k];\n                            if (dik != inf) cplib_warshall_floyd_relax_avx2(\n                                d[i], row_k, dik, kk, kend, inf4, inf);\n                        }\n                    }\n                }\n\n                // Phase 3: update all remaining blocks while the three tiles\n                // stay cache-resident, reusing them across the inner loops.\n                for (std::size_t ii = 0; ii < n; ii += block_size) {\n                    if (ii == kk) continue;\n                    const std::size_t iend =\n                        ii + block_size < n ? ii + block_size : n;\n                    for (std::size_t jj = 0; jj < n; jj += block_size) {\n                        if (jj == kk) continue;\n                        const std::size_t jend =\n                            jj + block_size < n ? jj + block_size : n;\n                        for (std::size_t k = kk; k < kend; ++k) {\n                            const std::int64_t* const row_k = d[k];\n                            for (std::size_t i = ii; i < iend; ++i) {\n                                const std::int64_t dik = d[i][k];\n                                if (dik != inf) cplib_warshall_floyd_relax_avx2(\n                                    d[i], row_k, dik, jj, jend, inf4, inf);\n                            }\n                        }\n                    }\n                }\n\n                for (std::size_t i = 0; i < n; ++i) {\n                    if (d[i][i] < zero) return true;\n                }\n            }\n            return false;\n        }\n\n        #pragma GCC pop_options\n\n        #endif\n        \"\"\".}\n\n        proc warshallFloydInt64Avx2(\n            rows: pointer,\n            n: csize_t,\n            zero, inf: int\n        ): bool {.importc: \"cplib_warshall_floyd_int64_avx2\".}\n\n    proc warshall_floyd_impl[T](g: DynamicGraph[T] or StaticGraph[T], zero, inf: T): tuple[negative_cycle: bool, d: seq[seq[T]]] =\n        var d = newSeqWith(g.len, newSeqWith(g.len, inf))\n        for i in 0..<g.len: d[i][i] = zero\n        for i in 0..<g.len:\n            for (j, cost) in g.to_and_cost(i):\n                d[i][j] = cost\n        for k in 0..<g.len:\n            for i in 0..<g.len:\n                for j in 0..<g.len:\n                    if d[i][k] != inf and d[k][j] != inf:\n                        d[i][j] = min(d[i][j], d[i][k] + d[k][j])\n            for i in 0..<g.len:\n                if d[i][i] < zero: return (negative_cycle: true, d: d)\n        return (negative_cycle: false, d: d)\n\n    proc warshall_floyd*(g: DynamicGraph[int] or StaticGraph[int], zero: int = 0, inf: int = INF64): tuple[negative_cycle: bool, d: seq[seq[int]]] =\n        when defined(cpp) and sizeof(int) == 8:\n            var d = newSeqWith(g.len, newSeqWith(g.len, inf))\n            for i in 0..<g.len: d[i][i] = zero\n            for i in 0..<g.len:\n                for (j, cost) in g.to_and_cost(i):\n                    d[i][j] = cost\n\n            if g.len == 0:\n                return (negative_cycle: false, d: d)\n\n            var rows = newSeq[ptr int](g.len)\n            for i in 0..<g.len:\n                rows[i] = addr d[i][0]\n            let negativeCycle = warshallFloydInt64Avx2(\n                cast[pointer](addr rows[0]), g.len.csize_t, zero, inf)\n            return (negative_cycle: negativeCycle, d: d)\n        else:\n            return warshall_floyd_impl(g, zero, inf)\n    proc warshall_floyd*(g: DynamicGraph[int32] or StaticGraph[int32], zero: int32 = 0.int32, inf: int32 = INF32): tuple[negative_cycle: bool, d: seq[seq[int32]]] = warshall_floyd_impl(g, zero, inf)\n    proc warshall_floyd*(g: DynamicGraph[float] or StaticGraph[float], zero: float = 0.0, inf: float = 1e100): tuple[negative_cycle: bool, d: seq[seq[float]]] = warshall_floyd_impl(g, zero, inf)\n    proc warshall_floyd*(g: DynamicGraph[float32] or StaticGraph[float32], zero: float32 = 0.0'f32, inf: float32 = 1e30'f32): tuple[negative_cycle: bool, d: seq[seq[float32]]] = warshall_floyd_impl(g, zero, inf)\n    proc warshall_floyd*[T](g: DynamicGraph[T] or StaticGraph[T], zero, inf: T): tuple[negative_cycle: bool, d: seq[seq[T]]] = warshall_floyd_impl(g, zero, inf)\n"
# source: src/cplib/graph/graph_debug.nim
ImportExpand "cplib/graph/graph_debug" <=== "when not declared CPLIB_GRAPH_GRAPHDEBUG:\n    const CPLIB_GRAPH_GRAPHDEBUG* = 1\n    import streams\n    import strformat\n    proc dump_graph*(G: WeightedDirectedGraph or WeightedDirectedStaticGraph,output:File=stdout)=\n        var M = 0\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                M += 1\n        output.writeLine($len(G)&\" \" & $M)\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                output.writeLine($x & \" \" & $y & \" \" & $c)\n    \n    proc dump_graph*(G: WeightedUnDirectedGraph or WeightedUnDirectedStaticGraph,output:File=stdout)=\n        var M = 0\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                if y >= x:\n                    M += 1\n        output.writeLine($len(G)&\" \" & $M)\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                if y >= x:\n                    output.writeLine($x & \" \" & $y &\" \" & $c)\n    \n    proc dump_graph*(G: UnWeightedDirectedGraph or UnWeightedDirectedStaticGraph,output:File=stdout)=\n        var M = 0\n        for x in 0..<len(G):\n            for y in G[x]:\n                M += 1\n        output.writeLine($len(G)&\" \" & $M)\n        for x in 0..<len(G):\n            for y in G[x]:\n                output.writeLine($x & \" \" & $y)\n    \n    proc dump_graph*(G: UnWeightedUnDirectedGraph or UnWeightedUnDirectedStaticGraph,output:File=stdout)=\n        var M = 0\n        for x in 0..<len(G):\n            for y in G[x]:\n                if y >= x:\n                    M += 1\n        output.writeLine($len(G)&\" \" & $M)\n        for x in 0..<len(G):\n            for y in G[x]:\n                if y >= x:\n                    output.writeLine($x & \" \" & $y)\n    \n    proc to_graph_graph*(G: WeightedDirectedGraph or WeightedDirectedStaticGraph,indexed:bool=false):string=\n        var M = 0\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                M += 1\n        result = fmt\"https://hello-world-494ec.firebaseapp.com/?format=normal&indexed={indexed}&weighted=true&directed=true&data={len(G)}+{M}\"\n        var add = 0\n        if indexed:\n            add += 1\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                result &= fmt\"%0A{x+add}+{y+add}+{c}\"\n\n    proc to_graph_graph*(G: WeightedUnDirectedGraph or WeightedUnDirectedStaticGraph,indexed:bool=false):string=\n        var M = 0\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                if y >= x:\n                    M += 1\n        result = fmt\"https://hello-world-494ec.firebaseapp.com/?format=normal&indexed={indexed}&weighted=true&directed=false&data={len(G)}+{M}\"\n        var add = 0\n        if indexed:\n            add += 1\n        for x in 0..<len(G):\n            for (y,c) in G[x]:\n                if y >= x:\n                    result &= fmt\"%0A{x+add}+{y+add}+{c}\"\n\n    proc to_graph_graph*(G: UnWeightedDirectedGraph or UnWeightedDirectedStaticGraph,indexed:bool=false):string=\n        var M = 0\n        for x in 0..<len(G):\n            for y in G[x]:\n                M += 1\n        result = fmt\"https://hello-world-494ec.firebaseapp.com/?format=normal&indexed={indexed}&weighted=false&directed=true&data={len(G)}+{M}\"\n        var add = 0\n        if indexed:\n            add += 1\n        for x in 0..<len(G):\n            for y in G[x]:\n                result &= fmt\"%0A{x+add}+{y+add}\"\n\n    proc to_graph_graph*(G: UnWeightedUnDirectedGraph or UnWeightedUnDirectedStaticGraph,indexed:bool=false):string=\n        var M = 0\n        for x in 0..<len(G):\n            for y in G[x]:\n                if y >= x:\n                    M += 1\n        result = fmt\"https://hello-world-494ec.firebaseapp.com/?format=normal&indexed={indexed}&weighted=false&directed=false&data={len(G)}+{M}\"\n        var add = 0\n        if indexed:\n            add += 1\n        for x in 0..<len(G):\n            for y in G[x]:\n                if y >= x:\n                    result &= fmt\"%0A{x+add}+{y+add}\"\n"


var N,M = ii()
var P = lii(N)
var G = initWeightedDirectedGraph(N)

for _ in range(M):
    var u,v,t = ii()
    u-=1
    v-=1
    G.add_edge(u,v,t)

var (f,res) = G.warshall_floyd()

assert not f

var mn = INF

for i in range(N):
    for j in range(N):
        if i != j:
            mn.min = res[i][j] + P[i] + P[j]

var cnt = 0

# echo G.to_graph_graph()

for i in range(N):
    for j in range(N):
        if i != j:
            if res[i][j] + P[i] + P[j] == mn:
                cnt += 1
                # print(i,j)

echo mn," ",cnt
0