結果

問題 No.417 チューリップバブル
ユーザー kimiyukikimiyuki
提出日時 2016-08-26 23:44:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 85,260 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-08 04:58:38
合計ジャッジ時間 4,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 23 ms
4,376 KB
testcase_12 AC 24 ms
4,376 KB
testcase_13 AC 10 ms
4,376 KB
testcase_14 AC 37 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 21 ms
4,376 KB
testcase_18 AC 20 ms
4,376 KB
testcase_19 AC 20 ms
4,380 KB
testcase_20 AC 76 ms
4,376 KB
testcase_21 AC 76 ms
4,376 KB
testcase_22 AC 75 ms
4,384 KB
testcase_23 AC 76 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 76 ms
4,376 KB
testcase_26 AC 8 ms
4,376 KB
testcase_27 AC 53 ms
4,376 KB
testcase_28 AC 76 ms
4,376 KB
testcase_29 AC 75 ms
4,380 KB
testcase_30 AC 75 ms
4,380 KB
testcase_31 AC 75 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 16 ms
4,376 KB
testcase_35 AC 152 ms
4,376 KB
testcase_36 AC 151 ms
4,376 KB
testcase_37 AC 153 ms
4,380 KB
testcase_38 AC 155 ms
4,376 KB
testcase_39 AC 154 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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