結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー ks2mks2m
提出日時 2022-10-14 21:59:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 782 ms / 2,000 ms
コード長 2,112 bytes
コンパイル時間 2,934 ms
コンパイル使用メモリ 75,948 KB
実行使用メモリ 86,604 KB
最終ジャッジ日時 2023-09-08 21:11:11
合計ジャッジ時間 27,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,332 KB
testcase_01 AC 42 ms
49,200 KB
testcase_02 AC 42 ms
49,320 KB
testcase_03 AC 42 ms
49,584 KB
testcase_04 AC 613 ms
77,784 KB
testcase_05 AC 514 ms
69,276 KB
testcase_06 AC 188 ms
58,176 KB
testcase_07 AC 224 ms
58,752 KB
testcase_08 AC 648 ms
79,020 KB
testcase_09 AC 510 ms
68,792 KB
testcase_10 AC 380 ms
63,980 KB
testcase_11 AC 469 ms
68,424 KB
testcase_12 AC 398 ms
64,392 KB
testcase_13 AC 410 ms
67,216 KB
testcase_14 AC 376 ms
62,532 KB
testcase_15 AC 452 ms
68,160 KB
testcase_16 AC 397 ms
61,820 KB
testcase_17 AC 321 ms
65,420 KB
testcase_18 AC 444 ms
70,720 KB
testcase_19 AC 651 ms
79,108 KB
testcase_20 AC 266 ms
58,564 KB
testcase_21 AC 389 ms
64,576 KB
testcase_22 AC 379 ms
61,688 KB
testcase_23 AC 594 ms
76,824 KB
testcase_24 AC 782 ms
82,300 KB
testcase_25 AC 702 ms
81,828 KB
testcase_26 AC 747 ms
82,004 KB
testcase_27 AC 750 ms
82,424 KB
testcase_28 AC 690 ms
81,960 KB
testcase_29 AC 750 ms
82,224 KB
testcase_30 AC 698 ms
82,288 KB
testcase_31 AC 694 ms
82,036 KB
testcase_32 AC 743 ms
82,220 KB
testcase_33 AC 744 ms
81,620 KB
testcase_34 AC 492 ms
65,280 KB
testcase_35 AC 451 ms
65,196 KB
testcase_36 AC 462 ms
64,712 KB
testcase_37 AC 434 ms
63,672 KB
testcase_38 AC 484 ms
65,200 KB
testcase_39 AC 431 ms
65,184 KB
testcase_40 AC 427 ms
65,004 KB
testcase_41 AC 417 ms
65,536 KB
testcase_42 AC 434 ms
64,840 KB
testcase_43 AC 429 ms
64,956 KB
testcase_44 AC 552 ms
86,096 KB
testcase_45 AC 544 ms
86,604 KB
testcase_46 AC 504 ms
84,712 KB
testcase_47 AC 518 ms
83,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++) {
			h[i] = Integer.parseInt(sa[i]);
		}
		List<List<Integer>> listL = new ArrayList<>(n);
		List<List<Integer>> listR = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			listL.add(new ArrayList<>());
			listR.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int x = Integer.parseInt(sa[0]) - 1;
			int y = Integer.parseInt(sa[1]) - 1;
			listL.get(x).add(y);
			listR.get(y).add(x);
		}
		br.close();

		long[] dplu = new long[n];
		long[] dpld = new long[n];
		long[] dpru = new long[n];
		long[] dprd = new long[n];
		Arrays.fill(dplu, -1);
		Arrays.fill(dpld, -1);
		Arrays.fill(dpru, -1);
		Arrays.fill(dprd, -1);
		dpld[0] = 0;
		dprd[n - 1] = 0;

		for (int i = 0; i < n - 1; i++) {
			if (dpld[i] != -1) {
				for (int j : listL.get(i)) {
					if (h[i] < h[j]) {
						dplu[j] = Math.max(dplu[j], dpld[i] + h[j] - h[i]);
					} else {
						dpld[j] = Math.max(dpld[j], dpld[i]);
					}
				}
			}
			if (dplu[i] != -1) {
				for (int j : listL.get(i)) {
					if (h[i] > h[j]) {
						dpld[j] = Math.max(dpld[j], dplu[i]);
					}
				}
			}
		}
		for (int i = n - 1; i > 0; i--) {
			if (dprd[i] != -1) {
				for (int j : listR.get(i)) {
					if (h[i] < h[j]) {
						dpru[j] = Math.max(dpru[j], dprd[i] + h[j] - h[i]);
					} else {
						dprd[j] = Math.max(dprd[j], dprd[i]);
					}
				}
			}
			if (dpru[i] != -1) {
				for (int j : listR.get(i)) {
					if (h[i] > h[j]) {
						dprd[j] = Math.max(dprd[j], dpru[i]);
					}
				}
			}
		}
		System.out.println(Math.max(dplu[n - 1], dpld[n - 1]));
		System.out.println(Math.max(dpru[0], dprd[0]));
	}
}
0