結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2022-10-14 22:38:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,608 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 201,764 KB
実行使用メモリ 14,000 KB
最終ジャッジ日時 2023-09-08 22:56:24
合計ジャッジ時間 10,396 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,076 KB
testcase_01 AC 3 ms
10,312 KB
testcase_02 AC 3 ms
9,644 KB
testcase_03 AC 3 ms
9,292 KB
testcase_04 AC 125 ms
12,036 KB
testcase_05 AC 101 ms
12,088 KB
testcase_06 AC 20 ms
9,188 KB
testcase_07 AC 24 ms
9,600 KB
testcase_08 AC 120 ms
12,228 KB
testcase_09 AC 100 ms
11,348 KB
testcase_10 AC 57 ms
9,964 KB
testcase_11 AC 91 ms
11,340 KB
testcase_12 AC 70 ms
10,780 KB
testcase_13 AC 73 ms
11,696 KB
testcase_14 AC 63 ms
10,136 KB
testcase_15 AC 79 ms
11,192 KB
testcase_16 AC 69 ms
10,480 KB
testcase_17 AC 48 ms
11,140 KB
testcase_18 AC 75 ms
11,340 KB
testcase_19 AC 143 ms
12,720 KB
testcase_20 AC 33 ms
10,264 KB
testcase_21 AC 68 ms
11,680 KB
testcase_22 AC 67 ms
10,852 KB
testcase_23 AC 110 ms
11,848 KB
testcase_24 AC 157 ms
13,252 KB
testcase_25 AC 156 ms
13,768 KB
testcase_26 AC 156 ms
13,144 KB
testcase_27 AC 155 ms
13,400 KB
testcase_28 AC 155 ms
13,800 KB
testcase_29 AC 156 ms
13,164 KB
testcase_30 AC 155 ms
14,000 KB
testcase_31 AC 157 ms
13,044 KB
testcase_32 AC 155 ms
13,628 KB
testcase_33 AC 156 ms
13,692 KB
testcase_34 AC 91 ms
11,284 KB
testcase_35 AC 83 ms
10,700 KB
testcase_36 AC 81 ms
12,076 KB
testcase_37 AC 77 ms
10,752 KB
testcase_38 AC 87 ms
11,108 KB
testcase_39 AC 69 ms
11,896 KB
testcase_40 AC 69 ms
11,676 KB
testcase_41 AC 70 ms
11,280 KB
testcase_42 AC 69 ms
10,968 KB
testcase_43 AC 72 ms
11,248 KB
testcase_44 AC 95 ms
13,396 KB
testcase_45 AC 95 ms
12,868 KB
testcase_46 AC 94 ms
12,844 KB
testcase_47 AC 98 ms
13,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main () {
    int N, M;
    cin >> N >> M;
    ll H[200020];
    for (int i = 0; i < N; i ++) {
        cin >> H[i];
    }
    vector<int> gr[100010];
    for (int i = 0; i < M; i ++) {
        int a, b;
        cin >> a >> b;
        gr[--a].push_back(--b);
        gr[b].push_back(a);
    }
    ll dp1[100010][2], dp2[100010][2];
    for (int i = 0; i < N; i ++) {
        for (int j = 0; j < 2; j ++) {
            dp1[i][j] = dp2[i][j] = -(ll)1e18;
        }
    }
    dp1[0][0] = dp1[0][1] = dp2[N-1][0] = dp2[N-1][1] = 0;
    for (int i = 0; i < N - 1; i ++) {
        for (int j = 0; j < 2; j ++) {
            for (auto v : gr[i]) {
                if (H[v] > H[i] && j) {
                    continue;
                }
                if (v <= i) {
                    continue;
                }
                int p = H[v] > H[i];
                dp1[v][p] = max(dp1[v][p], dp1[i][j] + max(0ll, H[v] - H[i]));
            }
        }
    }
    for (int i = N - 1; i > 0; i --) {
        for (int j = 0; j < 2; j ++) {
            for (auto v : gr[i]) {
                if (H[v] > H[i] && j) {
                    continue;
                }
                if (v >= i) {
                    continue;
                }
                int p = H[v] > H[i];
                dp2[v][p] = max(dp2[v][p], dp2[i][j] + max(0ll, H[v] - H[i]));
            }
        }
    }
    ll ans1 = max(dp1[N-1][0], dp1[N-1][1]), ans2 = max(dp2[0][0], dp2[0][1]);
    cout << (ans1 < 0 ? -1 : ans1) << '\n' << (ans2 < 0 ? -1 : ans2) << endl;
}
0