結果
| 問題 |
No.417 チューリップバブル
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-05 10:34:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 157 ms / 2,000 ms |
| コード長 | 1,493 bytes |
| コンパイル時間 | 1,555 ms |
| コンパイル使用メモリ | 85,768 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 23:25:17 |
| 合計ジャッジ時間 | 3,993 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#include <algorithm>
#include <functional>
#include <iostream>
#include <tuple>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define whole(f, x, ...) ([&](decltype((x)) y) { return (f)(begin(y), end(y), ##__VA_ARGS__); })(x)
typedef long long ll;
using namespace std;
template <class T>
void setmax(T &a, T const &b)
{
if (a < b)
a = b;
}
const ll inf = ll(1e18) + 9;
int main()
{
int n, m;
cin >> n >> m;
vector<int> u(n);
repeat(i, n) cin >> u[i];
vector<vector<pair<int, int>>> g(n);
repeat(i, n - 1)
{
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
function<vector<ll>(int, int)> dfs = [&](int i, int p) {
vector<ll> cur(m / 2 + 1, -inf);
cur[0] = u[i];
for (auto it : g[i]) {
int j, c;
tie(j, c) = it;
if (j == p)
continue;
vector<ll> nxt(m / 2 + 1, -inf);
vector<ll> prv = dfs(j, i);
repeat(x, m / 2 + 1)
{
setmax(nxt[x], cur[x]);
repeat(y, m / 2 + 1)
{
if (x + y + c >= m / 2 + 1)
break;
setmax(nxt[x + y + c], cur[x] + prv[y]);
}
}
cur.swap(nxt);
}
return cur;
};
vector<ll> dp = dfs(0, -1);
cout << *whole(max_element, dp) << endl;
}