結果
問題 | No.417 チューリップバブル |
ユーザー |
![]() |
提出日時 | 2020-03-25 09:46:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 867 ms / 2,000 ms |
コード長 | 980 bytes |
コンパイル時間 | 1,846 ms |
コンパイル使用メモリ | 174,856 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2025-01-01 19:58:33 |
合計ジャッジ時間 | 12,682 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define rep(i,n) for(int i = 0; i < (n);i++)#define sz(x) int(x.size())typedef long long ll;typedef pair<int,int> P;int main(){int n, m;cin >> n >> m;vector<int> U(n);rep(i,n) cin >> U[i];vector<vector<P>> g(n);rep(i,n-1) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(make_pair(b, c));g[b].emplace_back(make_pair(a, c));}vector<vector<ll>> dp(n, vector<ll>(m+1,0));auto dfs = [&](auto& f, int p, int u)->void{dp[u][0] = U[u];for (auto e : g[u]) {if (e.first == p) continue;int v = e.first, cost = e.second;f(f, u, v);for (int i = m; i >= 0; i--) {for (int j = 0; j <= m; j++) {int t = i + cost * 2 + j;if (t <= m) dp[u][t] = max(dp[u][t], dp[u][i] + dp[v][j]);}}}};dfs(dfs, -1, 0);ll res = 0;rep(i,m+1) res = max(res, dp[0][i]);cout << res << endl;return 0;}