結果

問題 No.417 チューリップバブル
ユーザー kimiyuki
提出日時 2016-08-26 23:44:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 85,672 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-08 10:45:37
合計ジャッジ時間 3,829 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <functional>
#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() {
    // input
    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);
    }
    // compute
    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);
    // output
    cout << *whole(max_element, dp) << endl;
    return 0;
}
0