結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー simansiman
提出日時 2022-10-18 08:38:23
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 4,304 ms
コンパイル使用メモリ 106,644 KB
実行使用メモリ 11,340 KB
最終ジャッジ日時 2023-09-11 03:51:10
合計ジャッジ時間 15,660 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 136 ms
8,868 KB
testcase_05 AC 108 ms
7,336 KB
testcase_06 AC 19 ms
4,628 KB
testcase_07 AC 23 ms
4,832 KB
testcase_08 AC 129 ms
10,296 KB
testcase_09 AC 104 ms
6,876 KB
testcase_10 AC 57 ms
4,724 KB
testcase_11 AC 96 ms
7,820 KB
testcase_12 AC 69 ms
5,956 KB
testcase_13 AC 75 ms
8,400 KB
testcase_14 AC 63 ms
5,456 KB
testcase_15 AC 81 ms
8,400 KB
testcase_16 AC 68 ms
5,600 KB
testcase_17 AC 50 ms
7,600 KB
testcase_18 AC 78 ms
8,660 KB
testcase_19 AC 155 ms
10,236 KB
testcase_20 AC 31 ms
4,996 KB
testcase_21 AC 69 ms
6,196 KB
testcase_22 AC 67 ms
5,472 KB
testcase_23 AC 121 ms
9,180 KB
testcase_24 AC 174 ms
11,324 KB
testcase_25 AC 177 ms
11,244 KB
testcase_26 AC 174 ms
11,232 KB
testcase_27 AC 174 ms
11,292 KB
testcase_28 AC 175 ms
11,300 KB
testcase_29 AC 177 ms
11,340 KB
testcase_30 AC 175 ms
11,296 KB
testcase_31 AC 175 ms
11,252 KB
testcase_32 AC 174 ms
11,296 KB
testcase_33 AC 176 ms
11,248 KB
testcase_34 AC 98 ms
6,152 KB
testcase_35 AC 87 ms
5,616 KB
testcase_36 AC 86 ms
5,484 KB
testcase_37 AC 81 ms
5,584 KB
testcase_38 AC 94 ms
5,688 KB
testcase_39 AC 73 ms
5,700 KB
testcase_40 AC 73 ms
5,940 KB
testcase_41 AC 73 ms
5,800 KB
testcase_42 AC 73 ms
5,688 KB
testcase_43 AC 74 ms
5,768 KB
testcase_44 AC 100 ms
11,024 KB
testcase_45 AC 101 ms
11,084 KB
testcase_46 AC 97 ms
10,960 KB
testcase_47 AC 96 ms
10,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<ll> H(N + 1);

  for (int i = 1; i <= N; ++i) {
    cin >> H[i];
  }

  vector<int> E[N + 1];
  for (int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;

    E[x].push_back(y);
    E[y].push_back(x);
  }

  ll dp[N + 1][2];
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;

  for (int i = 1; i < N; ++i) {
    for (int k = 0; k < 2; ++k) {
      if (dp[i][k] == -1) continue;

      for (int j : E[i]) {
        if (j < i) continue;
        if (k == 1 && H[i] < H[j]) continue;

        if (H[i] < H[j]) {
          dp[j][1] = max(dp[j][1], dp[i][k] + abs(H[j] - H[i]));
        } else {
          dp[j][0] = max(dp[j][0], dp[i][k]);
        }
      }
    }
  }

  cout << max(dp[N][0], dp[N][1]) << endl;

  memset(dp, -1, sizeof(dp));
  dp[N][0] = 0;

  for (int i = N; i > 1; --i) {
    for (int k = 0; k < 2; ++k) {
      if (dp[i][k] == -1) continue;

      for (int j : E[i]) {
        if (i < j) continue;
        if (k == 1 && H[j] > H[i]) continue;

        if (H[i] < H[j]) {
          dp[j][1] = max(dp[j][1], dp[i][k] + abs(H[j] - H[i]));
        } else {
          dp[j][0] = max(dp[j][0], dp[i][k]);
        }
      }
    }
  }

  cout << max(dp[1][0], dp[1][1]) << endl;

  return 0;
}
0