結果

問題 No.417 チューリップバブル
ユーザー nebukuro09
提出日時 2017-05-30 23:06:05
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 1,437 bytes
コンパイル時間 879 ms
コンパイル使用メモリ 123,776 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-06-12 19:33:54
合計ジャッジ時間 26,076 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 28 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;


alias Tuple!(int, "to", int, "cost") Edge;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto U = N.iota.map!(_ => readln.chomp.to!int).array;
    auto edges = new Edge[][](N);
    foreach (i; 0..N-1) {
        s = readln.split.map!(to!int);
        edges[s[0]] ~= Edge(s[1], s[2]);
        edges[s[1]] ~= Edge(s[0], s[2]);
    }


    auto dp = new int[][](N, M + 1);

    void dfs(int n, int prev) {
        foreach (m; edges[n])
            if (m.to != prev)
                dfs(m.to, n);

        auto tmp = new int[][](2, M + 1);
        int cur = 0;
        int tar = 1;

        foreach (m; edges[n]) {
            if (m.to == prev)
                continue;
            foreach (i; 0..M+1) {
                int ii = i + m.cost * 2;
                foreach (j; 0..M+1) {
                    if (ii + j > M)
                        break;
                    tmp[tar][ii+j] = max(tmp[cur][ii+j], dp[m.to][i] + tmp[cur][j]);
                }
            }

            fill(tmp[cur], 0);
            cur ^= 1;
            tar ^= 1;
        }

        //if (n == 0) tmp[cur].writeln;
        foreach (i; 0..M+1) dp[n][i] = tmp[cur][i] + U[n];
    }


    dfs(0, -1);
    dp[0].reduce!max.writeln;
}
0