結果

問題 No.417 チューリップバブル
ユーザー snrnsidy
提出日時 2021-04-28 00:59:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 437 ms / 2,000 ms
コード長 1,457 bytes
コンパイル時間 5,877 ms
コンパイル使用メモリ 128,924 KB
最終ジャッジ日時 2025-01-21 01:32:23
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>
#include <random>

using namespace std;

int dp[201][2001];
int U[201];
vector <pair<int, int>> adj[201];
int N, M, A, B, C;

void solve(int now, int prev)
{
	dp[now][0] = U[now];
	for (auto it : adj[now])
	{
		int cost = it.second;
		int next = it.first;
		if (prev == next)
		{
			continue;
		}
		solve(next, now);
		for (int j = M; j >= 0; j--)
		{
			if (dp[now][j] == -1)
			{
				continue;
			}
			for (int k = M; k >= 0; k--)
			{
				if (j + k + 2 * cost <= M)
				{
					dp[now][j + k + 2 * cost] = max(dp[now][j + k + 2 * cost], dp[now][j] + dp[next][k]);
				}
			}
		}
	}
	return;
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	cin >> N >> M;

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

	for (int i = 0; i < N - 1; i++)
	{
		cin >> A >> B >> C;
		adj[A].push_back(make_pair(B, C));
		adj[B].push_back(make_pair(A, C));
	}

	memset(dp, -1, sizeof(dp));

	solve(0, -1);
	
	int res = 0;	

	for (int i = 0; i <= M; i++)
	{
		res = max(res, dp[0][i]);
	}

	cout << res << '\n';

	return 0;
}
0