結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー srjywrdnprkt
提出日時 2023-06-11 04:38:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,355 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 112,656 KB
最終ジャッジ日時 2025-02-14 01:30:19
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;
using ll = long long;

int main(){

    ll N, M, X, Y, ans;
    cin >> N >> M;
    vector<ll> H(N);
    for (int i=0; i<N; i++) cin >> H[i];
    vector<vector<ll>> E(N);

    for (int i=0; i<M; i++){
        cin >> X >> Y; X--; Y--;
        E[X].push_back(Y);
        E[Y].push_back(X);
    }

    vector<vector<ll>> dp(N, vector<ll>(2, -1e18)), dp2(N, vector<ll>(2, -1e18));
    dp[0][0] = 0; dp2[N-1][0] = 0;
    
    for (int i=1; i<N; i++){
        for (auto k : E[i]){
            if (H[k] > H[i] && k < i) dp[i][0] = max({dp[i][0], dp[k][0], dp[k][1]});
            if (H[k] < H[i] && k < i) dp[i][1] = max(dp[i][1], dp[k][0]+H[i]-H[k]);
        }
    }
    ans = max(dp[N-1][0], dp[N-1][1]);
    if (ans == -1e18) ans = -1;
    cout << ans << endl;

    for (int i=N-1; i>=0; i--){
        for (auto k : E[i]){
            if (H[k] > H[i] && k > i) dp2[i][0] = max({dp2[i][0], dp2[k][0], dp2[k][1]});
            if (H[k] < H[i] && k > i) dp2[i][1] = max(dp2[i][1], dp2[k][0]+H[i]-H[k]);
        }
    }
    ans = max(dp2[0][0], dp2[0][1]);
    if (ans < 0) ans = -1;
    cout << ans << endl;

    return 0;
}
0