結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー hourenhouren
提出日時 2022-10-14 21:49:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,599 bytes
コンパイル時間 1,755 ms
コンパイル使用メモリ 176,336 KB
実行使用メモリ 13,724 KB
最終ジャッジ日時 2024-06-26 13:40:34
合計ジャッジ時間 5,785 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define fix(x) fixed << setprecision(x)
#define asc(x) x, vector<x>, greater<x>
#define rep(i, n) for(ll i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;}
const ll INF = (1LL<<60);
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<ll> h(n);
    rep(i,n) cin >> h[i];
    vector<vector<int>> to(n);
    rep(i,m){
        int x,y;
        cin >> x >> y;
        x--; y--;
        to[x].push_back(y);
        to[y].push_back(x);
    }
    vector<vector<ll>> dp(2,vector<ll>(n,-INF)), dp2(2,vector<ll>(n,-INF));
    dp[0][0] = dp[1][0] = dp2[0][n-1] = dp2[1][n-1] = 0;
    rep(i,n){
        for(int x:to[i]){
            if(i<x){
                if(h[i]<h[x]) chmax(dp[1][x], dp[0][i]+h[x]-h[i]);
                else{
                    chmax(dp[0][x], dp[1][i]);
                    chmax(dp[0][x], dp[0][i]);
                }
            }
        }
    }
    for(int i=n-1;i>=0;i--){
        for(int x:to[i]){
            if(i>x){
                if(h[i]<h[x]) chmax(dp2[1][x], dp2[0][i]+h[x]-h[i]);
                else{
                    chmax(dp2[0][x], dp2[1][i]);
                    chmax(dp2[0][x], dp2[0][i]);
                }
            }
        }
    }
    cout << max<ll>({-1,dp[0][n-1],dp[1][n-1]}) << "\n" << max<ll>({-1,dp2[0][0],dp2[1][0]}) << '\n';
    return 0;
}
0