結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー 👑 kakel-sankakel-san
提出日時 2022-10-14 23:19:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 419 ms / 2,000 ms
コード長 2,052 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 64,164 KB
実行使用メモリ 46,852 KB
最終ジャッジ日時 2023-09-08 23:41:42
合計ジャッジ時間 18,349 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
21,988 KB
testcase_01 AC 66 ms
23,940 KB
testcase_02 AC 65 ms
21,796 KB
testcase_03 AC 65 ms
21,732 KB
testcase_04 AC 342 ms
41,844 KB
testcase_05 AC 295 ms
41,032 KB
testcase_06 AC 106 ms
28,280 KB
testcase_07 AC 115 ms
30,928 KB
testcase_08 AC 304 ms
44,372 KB
testcase_09 AC 301 ms
37,320 KB
testcase_10 AC 241 ms
32,984 KB
testcase_11 AC 256 ms
36,752 KB
testcase_12 AC 229 ms
34,940 KB
testcase_13 AC 196 ms
37,984 KB
testcase_14 AC 229 ms
31,072 KB
testcase_15 AC 216 ms
38,016 KB
testcase_16 AC 249 ms
33,952 KB
testcase_17 AC 145 ms
35,888 KB
testcase_18 AC 204 ms
40,184 KB
testcase_19 AC 384 ms
42,344 KB
testcase_20 AC 136 ms
31,940 KB
testcase_21 AC 222 ms
32,732 KB
testcase_22 AC 241 ms
31,184 KB
testcase_23 AC 301 ms
42,200 KB
testcase_24 AC 419 ms
44,848 KB
testcase_25 AC 418 ms
43,004 KB
testcase_26 AC 410 ms
46,852 KB
testcase_27 AC 413 ms
42,832 KB
testcase_28 AC 403 ms
40,820 KB
testcase_29 AC 409 ms
42,768 KB
testcase_30 AC 409 ms
42,892 KB
testcase_31 AC 406 ms
44,884 KB
testcase_32 AC 401 ms
44,836 KB
testcase_33 AC 409 ms
42,768 KB
testcase_34 AC 342 ms
35,644 KB
testcase_35 AC 334 ms
34,712 KB
testcase_36 AC 336 ms
34,692 KB
testcase_37 AC 330 ms
32,608 KB
testcase_38 AC 335 ms
33,060 KB
testcase_39 AC 317 ms
34,236 KB
testcase_40 AC 318 ms
32,232 KB
testcase_41 AC 318 ms
32,136 KB
testcase_42 AC 319 ms
34,184 KB
testcase_43 AC 319 ms
34,132 KB
testcase_44 AC 246 ms
43,412 KB
testcase_45 AC 245 ms
39,156 KB
testcase_46 AC 242 ms
38,768 KB
testcase_47 AC 244 ms
40,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var h = NList;
        var map = NMap(m);

        Array.Sort(map, (l, r) => l[0].CompareTo(r[0]));
        var dp = new long[2][];
        for (var i = 0; i < dp.Length; ++i) dp[i] = Enumerable.Repeat(long.MinValue, n).ToArray();
        dp[1][0] = 0;
        foreach (var stair in map)
        {
            if (h[stair[0]] < h[stair[1]])
            {
                dp[0][stair[1]] = Math.Max(dp[0][stair[1]], dp[1][stair[0]] + h[stair[1]] - h[stair[0]]);
            }
            else
            {
                dp[1][stair[1]] = Math.Max(dp[1][stair[1]], Math.Max(dp[0][stair[0]], dp[1][stair[0]]));
            }
        }
        var ch = Math.Max(dp[0][n - 1], dp[1][n - 1]);
        if (ch < 0) ch = -1;

        Array.Sort(map, (l, r) => r[1].CompareTo(l[1]));
        dp = new long[2][];
        for (var i = 0; i < dp.Length; ++i) dp[i] = Enumerable.Repeat(long.MinValue, n).ToArray();
        dp[1][n - 1] = 0;
        foreach (var stair in map)
        {
            if (h[stair[0]] > h[stair[1]])
            {
                dp[0][stair[0]] = Math.Max(dp[0][stair[0]], dp[1][stair[1]] + h[stair[0]] - h[stair[1]]);
            }
            else
            {
                dp[1][stair[0]] = Math.Max(dp[1][stair[0]], Math.Max(dp[0][stair[1]], dp[1][stair[1]]));
            }
        }
        var ze = Math.Max(dp[0][0], dp[1][0]);
        if (ze < 0) ze = -1;
        WriteLine(ch);
        WriteLine(ze);
    }
}
0