結果

問題 No.417 チューリップバブル
ユーザー warabi0906warabi0906
提出日時 2023-02-06 12:24:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 2,103 bytes
コンパイル時間 3,844 ms
コンパイル使用メモリ 232,068 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 00:15:47
合計ジャッジ時間 5,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 15 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 11 ms
4,380 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 26 ms
4,380 KB
testcase_21 AC 24 ms
4,380 KB
testcase_22 AC 23 ms
4,380 KB
testcase_23 AC 28 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 18 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 26 ms
4,380 KB
testcase_28 AC 27 ms
4,380 KB
testcase_29 AC 25 ms
4,380 KB
testcase_30 AC 44 ms
4,380 KB
testcase_31 AC 34 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 50 ms
4,380 KB
testcase_36 AC 42 ms
4,376 KB
testcase_37 AC 32 ms
4,380 KB
testcase_38 AC 24 ms
4,380 KB
testcase_39 AC 15 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
	if ('A' <= c and c <= 'Z')return (int)(c - 'A');
	if ('a' <= c and c <= 'z')return (int)(c - 'a');
	if ('0' <= c and c <= '9')return (int)(c - '0');
	assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 62);
using minit = modint998244353;

template<typename T>
ostream& operator << (ostream& st, const vector<T> &v) {
	for (const T value : v) {
		st << value << " ";
	}
	return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
	for (T &value : v) {
		st >> value;
	}
	return st;
}

//
typedef struct {
	int to, cost;
}edge;
//

void solve() {
	//#1 入力
	int N, M;
	cin >> N >> M;
	vector<int> U(N);
	cin >> U;
	vector<vector<edge>> G(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v, c;
		cin >> u >> v >> c;
		G[u].push_back({ v,c });
		G[v].push_back({ u,c });
	}

	//#2 再帰的に答えを求める
	function<vector<int>(int, int)> dfs = [&](int now, int par) {
		//頂点nowから探索を(子方向に)始めてちょうど時間iで徴収できる最大税
		vector<int> res(M + 1, -1);
		res[0] = U[now];
		for (const edge e : G[now]) {
			if (e.to == par)continue;
			vector<int> prev = dfs(e.to, now);
			//cout << "edge " << now << "to " << e.to << "cost is " << e.cost << endl;
			for (int i = M; i >= 0; i--) {
				if (res[i] == -1)continue;
				for (int j = 0; j <= M; j++) {
					if (prev[j] == -1)continue;
					if (M < i + j + e.cost * 2)continue;
					chmax(res[i + j + e.cost * 2], res[i] + prev[j]);
				}
			}
		}
		return res;
	};

	//#3 出力
	vector<int> ans = dfs(0, -1);
	cout << *max_element(ans.begin(), ans.end()) << endl;
}
signed main() {
	int T = 1;
	// cin >> T;
	for (int i = 0; i < T; i++)
		solve();
	return 0;
}
0