結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー aqua
提出日時 2026-08-30 15:46:10
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 92 ms / 2,000 ms
+ 567µs
コード長 1,016 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,237 ms
コンパイル使用メモリ 346,664 KB
実行使用メモリ 18,664 KB
最終ジャッジ日時 2026-08-30 15:46:27
合計ジャッジ時間 5,157 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }

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

    int N; cin >> N;
    vector<ll> S(N);
    for (auto &&s : S) cin >> s;

    vector<vector<int>> G(N);
    for (int i = 1; i < N; ++i) {
        int u, v; cin >> u >> v; --u, --v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int l, int r) { return S[l] < S[r]; });
    ll ans = -1;
    vector<ll> dp(N, -1);
    auto dfs = [&](auto self, int u) -> ll {
        if (!chmax(dp[u], 0)) return dp[u];
        for (int &v : G[u]) {
            if (S[v] > S[u]) chmax(dp[u], self(self, v));
        }
        return dp[u] += S[u];
    };
    for (int i = N; i--; ) {
        chmax(ans, dfs(dfs, ord[i]));
    }
    cout << ans << '\n';
}
0