結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー kyskys
提出日時 2022-10-14 21:44:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 1,738 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 115,224 KB
最終ジャッジ日時 2023-09-08 20:43:38
合計ジャッジ時間 17,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,548 KB
testcase_01 AC 91 ms
71,540 KB
testcase_02 AC 91 ms
71,200 KB
testcase_03 AC 90 ms
71,548 KB
testcase_04 AC 397 ms
102,168 KB
testcase_05 AC 300 ms
96,228 KB
testcase_06 AC 173 ms
84,056 KB
testcase_07 AC 185 ms
84,756 KB
testcase_08 AC 401 ms
109,492 KB
testcase_09 AC 285 ms
93,272 KB
testcase_10 AC 194 ms
83,816 KB
testcase_11 AC 315 ms
98,324 KB
testcase_12 AC 240 ms
89,372 KB
testcase_13 AC 271 ms
101,376 KB
testcase_14 AC 222 ms
86,240 KB
testcase_15 AC 295 ms
106,040 KB
testcase_16 AC 229 ms
87,304 KB
testcase_17 AC 235 ms
98,760 KB
testcase_18 AC 276 ms
102,080 KB
testcase_19 AC 442 ms
115,224 KB
testcase_20 AC 205 ms
86,464 KB
testcase_21 AC 249 ms
90,244 KB
testcase_22 AC 227 ms
86,200 KB
testcase_23 AC 358 ms
110,332 KB
testcase_24 AC 463 ms
112,944 KB
testcase_25 AC 488 ms
113,036 KB
testcase_26 AC 482 ms
113,544 KB
testcase_27 AC 467 ms
113,144 KB
testcase_28 AC 495 ms
113,144 KB
testcase_29 AC 495 ms
113,356 KB
testcase_30 AC 499 ms
113,532 KB
testcase_31 AC 477 ms
113,224 KB
testcase_32 AC 473 ms
113,288 KB
testcase_33 AC 454 ms
112,392 KB
testcase_34 AC 248 ms
88,708 KB
testcase_35 AC 230 ms
87,272 KB
testcase_36 AC 226 ms
86,844 KB
testcase_37 AC 218 ms
86,852 KB
testcase_38 AC 240 ms
88,316 KB
testcase_39 AC 193 ms
86,220 KB
testcase_40 AC 191 ms
86,080 KB
testcase_41 AC 188 ms
85,900 KB
testcase_42 AC 191 ms
85,992 KB
testcase_43 AC 194 ms
85,628 KB
testcase_44 AC 231 ms
113,808 KB
testcase_45 AC 225 ms
113,660 KB
testcase_46 AC 216 ms
107,700 KB
testcase_47 AC 216 ms
107,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def main():
    from sys import stdin, setrecursionlimit
    # setrecursionlimit(1000000)
    input = stdin.readline
    def iinput(): return int(input())
    def sinput(): return input().rstrip()
    def i0input(): return int(input()) - 1
    def linput(): return list(input().split())
    def liinput(): return list(map(int, input().split()))
    def miinput(): return map(int, input().split())
    def li0input(): return list(map(lambda x: int(x) - 1, input().split()))
    def mi0input(): return map(lambda x: int(x) - 1, input().split())
    INF = 1000000000000000000
    MOD = 1000000007

    N, M = miinput()
    H = liinput()

    G = [[] for _ in [0] * N]
    RG = [[] for _ in [0] * N]
    DEG = [0] * N
    RDEG = [0] * N

    for _ in [0] * M:
        x, y = mi0input()
        G[x].append(y)
        DEG[y] += 1
        RG[y].append(x)
        RDEG[x] += 1
    
    def solve(g, s, deg):
        dp = [[-INF, -INF] for _ in [0] * N]
        dp[s][0] = 0
        dp[s][1] = 0
        que = deque()

        for i in range(N):
            if deg[i] == 0:
                que.append(i)
        
        while que:
            u = que.popleft()
            for v in g[u]:
                if H[v] > H[u]:
                    dp[v][1] = max(dp[v][1], dp[u][0] + H[v] - H[u])
                else:
                    dp[v][0] = max(dp[v][0], dp[u][0], dp[u][1])
                deg[v] -= 1
                if deg[v] == 0:
                    que.append(v)
        
        return dp
    
    ans = max(solve(G, 0, DEG)[-1])
    if ans >= 0:
        print(ans)
    else:
        print(-1)

    ans = max(solve(RG, N-1, RDEG)[0])
    if ans >= 0:
        print(ans)
    else:
        print(-1)

main()
0