結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー umimelumimel
提出日時 2022-10-14 23:02:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,893 bytes
コンパイル時間 1,665 ms
コンパイル使用メモリ 173,188 KB
実行使用メモリ 21,720 KB
最終ジャッジ日時 2023-09-08 23:25:09
合計ジャッジ時間 8,926 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 139 ms
15,648 KB
testcase_05 AC 111 ms
12,672 KB
testcase_06 AC 22 ms
5,752 KB
testcase_07 AC 26 ms
6,368 KB
testcase_08 AC 139 ms
19,616 KB
testcase_09 AC 106 ms
10,840 KB
testcase_10 AC 52 ms
5,780 KB
testcase_11 AC 100 ms
13,760 KB
testcase_12 AC 74 ms
8,988 KB
testcase_13 AC 83 ms
15,856 KB
testcase_14 AC 64 ms
6,940 KB
testcase_15 AC 89 ms
15,040 KB
testcase_16 AC 70 ms
7,180 KB
testcase_17 AC 59 ms
14,208 KB
testcase_18 AC 87 ms
16,104 KB
testcase_19 AC 161 ms
19,184 KB
testcase_20 AC 36 ms
6,984 KB
testcase_21 AC 73 ms
9,316 KB
testcase_22 AC 68 ms
6,860 KB
testcase_23 AC 128 ms
17,100 KB
testcase_24 AC 189 ms
21,652 KB
testcase_25 AC 186 ms
21,660 KB
testcase_26 AC 183 ms
21,508 KB
testcase_27 AC 178 ms
21,600 KB
testcase_28 AC 179 ms
21,588 KB
testcase_29 AC 181 ms
21,612 KB
testcase_30 AC 180 ms
21,416 KB
testcase_31 AC 177 ms
21,720 KB
testcase_32 AC 176 ms
21,628 KB
testcase_33 AC 180 ms
21,596 KB
testcase_34 AC 99 ms
7,916 KB
testcase_35 AC 83 ms
7,676 KB
testcase_36 AC 82 ms
7,368 KB
testcase_37 AC 75 ms
7,384 KB
testcase_38 AC 88 ms
7,264 KB
testcase_39 AC 70 ms
7,960 KB
testcase_40 AC 70 ms
7,916 KB
testcase_41 AC 67 ms
8,200 KB
testcase_42 AC 67 ms
7,976 KB
testcase_43 AC 67 ms
8,072 KB
testcase_44 AC 112 ms
20,272 KB
testcase_45 AC 111 ms
20,068 KB
testcase_46 AC 110 ms
20,272 KB
testcase_47 AC 109 ms
20,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

int main(){
    ll n, m;
    cin >> n >> m;

    vector<ll> h(n);
    rep(i, n) cin >> h[i];

    vector<vector<ll>> G(n);
    rep(i, m){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        G[u].pb(v);
        G[v].pb(u);
    }

    // cherry
    vector<vector<ll>> cost1(n, vector<ll>(2, -INF));
    cost1[0][0] = 0;
    for(ll v=1; v<n; v++){
        for(ll u : G[v]){
            if(v < u) continue;
            if(h[u] < h[v]){
                if(cost1[u][0] != -INF) cost1[v][1] = max(cost1[v][1], cost1[u][0]+(h[v]-h[u]));
            }else{
                if(cost1[u][0] != -INF) cost1[v][0] = max(cost1[v][0], cost1[u][0]);
                if(cost1[u][1] != -INF) cost1[v][0] = max(cost1[v][0], cost1[u][1]);
            }
        }
    }

    vector<vector<ll>> cost2(n, vector<ll>(2, -INF));
    cost2[n-1][0] = 0;
    for(ll v=n-2; v>=0; v--){
        for(ll u : G[v]){
            if(u < v) continue;
            if(h[u] < h[v]){
                if(cost2[u][0] != -INF) cost2[v][1] = max(cost2[v][1], cost2[u][0]+(h[v]-h[u]));
            }else{
                if(cost2[u][0] != -INF) cost2[v][0] = max(cost2[v][0], cost2[u][0]);
                if(cost2[u][1] != -INF) cost2[v][0] = max(cost2[v][0], cost2[u][1]);
            }
        }
    }

    ll che = max(cost1[n-1][0], cost1[n-1][1]);
    ll zer = max(cost2[0][0], cost2[0][1]);

    if(che == -INF) cout << -1 << endl;
    else cout << che << endl;

    if(zer == -INF) cout << -1 << endl;
    else cout << zer << endl;
}
0