結果

問題 No.417 チューリップバブル
ユーザー te-shte-sh
提出日時 2017-10-26 15:59:45
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 96,228 KB
実行使用メモリ 4,884 KB
最終ジャッジ日時 2023-09-03 16:28:58
合計ジャッジ時間 2,773 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 6 ms
4,504 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 15 ms
4,380 KB
testcase_21 AC 13 ms
4,376 KB
testcase_22 AC 13 ms
4,376 KB
testcase_23 AC 15 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 11 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 14 ms
4,376 KB
testcase_28 AC 15 ms
4,384 KB
testcase_29 AC 15 ms
4,380 KB
testcase_30 AC 23 ms
4,376 KB
testcase_31 AC 18 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,884 KB
testcase_35 AC 27 ms
4,380 KB
testcase_36 AC 23 ms
4,388 KB
testcase_37 AC 18 ms
4,388 KB
testcase_38 AC 14 ms
4,380 KB
testcase_39 AC 9 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

void main()
{
  auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1]/2;
  auto u = new int[](n);
  foreach (i; 0..n) u[i] = readln.chomp.to!int;

  struct Edge { size_t a, b; int c; }
  auto e = new Edge[][](n);
  foreach (i; 0..n-1) {
    auto rd2 = readln.splitter;
    auto a = rd2.front.to!size_t; rd2.popFront();
    auto b = rd2.front.to!size_t; rd2.popFront();
    auto c = rd2.front.to!int;
    e[a] ~= Edge(a, b, c);
    e[b] ~= Edge(b, a, c);
  }

  auto parent = new size_t[](n);
  parent[0] = n;
  auto q = DList!size_t(0), s = SList!size_t();
  while (!q.empty) {
    auto a = q.front; q.removeFront();
    s.insertFront(a);
    foreach (ei; e[a])
      if (ei.b != parent[a]) {
        parent[ei.b] = a;
        q.insertBack(ei.b);
      }
  }

  auto dp = new int[][](n, m+1);
  while (!s.empty) {
    auto a = s.front; s.removeFront();
    dp[a][0] = u[a];
    foreach (ei; e[a])
      if (ei.b != parent[a])
        foreach_reverse (i; 0..m+1)
          if (dp[a][i])
            foreach_reverse (j; 0..m+1)
              if (dp[ei.b][j] && i+j+ei.c <= m)
                dp[a][i+j+ei.c] = max(dp[a][i+j+ei.c], dp[a][i] + dp[ei.b][j]);
  }

  writeln(dp[0].maxElement);
}
0