結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー snow39snow39
提出日時 2022-10-14 21:45:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,946 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 117,096 KB
実行使用メモリ 17,312 KB
最終ジャッジ日時 2023-09-08 20:43:45
合計ジャッジ時間 5,950 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,388 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 65 ms
12,864 KB
testcase_05 AC 51 ms
10,212 KB
testcase_06 AC 11 ms
5,232 KB
testcase_07 AC 14 ms
5,708 KB
testcase_08 AC 67 ms
15,264 KB
testcase_09 AC 47 ms
9,140 KB
testcase_10 AC 25 ms
5,596 KB
testcase_11 AC 50 ms
11,184 KB
testcase_12 AC 34 ms
7,608 KB
testcase_13 AC 43 ms
12,260 KB
testcase_14 AC 29 ms
6,424 KB
testcase_15 AC 46 ms
11,796 KB
testcase_16 AC 31 ms
6,552 KB
testcase_17 AC 31 ms
10,668 KB
testcase_18 AC 44 ms
12,352 KB
testcase_19 AC 78 ms
15,252 KB
testcase_20 AC 17 ms
6,276 KB
testcase_21 AC 34 ms
8,140 KB
testcase_22 AC 30 ms
6,448 KB
testcase_23 AC 61 ms
13,404 KB
testcase_24 AC 84 ms
17,028 KB
testcase_25 AC 86 ms
17,312 KB
testcase_26 AC 86 ms
17,024 KB
testcase_27 AC 85 ms
17,152 KB
testcase_28 AC 84 ms
17,100 KB
testcase_29 AC 85 ms
17,096 KB
testcase_30 AC 85 ms
17,076 KB
testcase_31 AC 85 ms
16,996 KB
testcase_32 AC 86 ms
17,028 KB
testcase_33 AC 86 ms
17,020 KB
testcase_34 AC 41 ms
7,312 KB
testcase_35 AC 36 ms
6,844 KB
testcase_36 AC 36 ms
6,856 KB
testcase_37 AC 34 ms
6,696 KB
testcase_38 AC 39 ms
6,972 KB
testcase_39 AC 30 ms
6,652 KB
testcase_40 AC 30 ms
6,556 KB
testcase_41 AC 31 ms
6,664 KB
testcase_42 AC 30 ms
6,540 KB
testcase_43 AC 30 ms
6,692 KB
testcase_44 AC 62 ms
17,192 KB
testcase_45 AC 61 ms
17,160 KB
testcase_46 AC 61 ms
17,164 KB
testcase_47 AC 61 ms
17,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

const ll INF = 1e18;

ll calc(vector<ll> H, vector<int> X, vector<int> Y) {
    const int N = H.size();
    const int M = X.size();
    vector<vector<int>> g(N);
    rep(i, M) g[X[i]].push_back(Y[i]);

    vector<vector<ll>> dp(N, vector<ll>(2, -INF));
    dp[0][0] = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 2; j++) {
            const ll value = dp[i][j];
            if (value == -INF) continue;

            for (auto to : g[i]) {
                if (j == 1 && H[i] < H[to]) continue;
                int nj = 0;
                if (H[i] < H[to]) nj = 1;
                chmax(dp[to][nj], value + max(0ll, H[to] - H[i]));
            }
        }
    }
    return max(dp[N - 1][0], dp[N - 1][1]);
}

void solve() {
    int N, M;
    cin >> N >> M;
    vector<ll> H(N);
    rep(i, N) cin >> H[i];
    vector<int> X(M), Y(M);
    rep(i, M) {
        cin >> X[i] >> Y[i];
        X[i]--;
        Y[i]--;
    }

    ll ans1 = calc(H, X, Y);
    reverse(all(H));
    rep(i, M) {
        X[i] = N - 1 - X[i];
        Y[i] = N - 1 - Y[i];
    }
    swap(X, Y);
    ll ans2 = calc(H, X, Y);

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

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0