結果

問題 No.417 チューリップバブル
ユーザー motakinemotakine
提出日時 2020-05-13 16:30:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 2,648 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 185,092 KB
実行使用メモリ 9,780 KB
最終ジャッジ日時 2023-10-12 17:00:33
合計ジャッジ時間 7,669 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 5 ms
4,352 KB
testcase_09 AC 9 ms
4,352 KB
testcase_10 AC 12 ms
4,352 KB
testcase_11 AC 47 ms
4,376 KB
testcase_12 AC 47 ms
4,428 KB
testcase_13 AC 18 ms
4,348 KB
testcase_14 AC 74 ms
5,028 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 42 ms
4,972 KB
testcase_18 AC 42 ms
4,908 KB
testcase_19 AC 42 ms
5,040 KB
testcase_20 AC 157 ms
6,492 KB
testcase_21 AC 155 ms
6,568 KB
testcase_22 AC 155 ms
6,620 KB
testcase_23 AC 155 ms
6,572 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 154 ms
6,544 KB
testcase_26 AC 16 ms
4,352 KB
testcase_27 AC 109 ms
5,552 KB
testcase_28 AC 154 ms
6,548 KB
testcase_29 AC 153 ms
6,724 KB
testcase_30 AC 154 ms
6,456 KB
testcase_31 AC 155 ms
6,624 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 5 ms
4,348 KB
testcase_34 AC 32 ms
5,216 KB
testcase_35 AC 317 ms
9,732 KB
testcase_36 AC 316 ms
9,652 KB
testcase_37 AC 314 ms
9,636 KB
testcase_38 AC 314 ms
9,780 KB
testcase_39 AC 316 ms
9,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
#define PRINT(v) for (auto x : (v)) cout <<x <<" " ; cout <<endl;
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using mat = vector<vector<ll>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
const int inf = 1001001001;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
  T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
void hey(){ cout <<"hey" <<endl; }

template<class T> struct edge { int to; T cost;};


int N,M;
vector<ll> U;
vector<vector<edge<int>>> G;
vector<vector<vector<ll>>> dp;


void input(){
  cin >>N >>M;
  U.assign(N, 0); rep(i, N) cin >>U[i];
  G.assign(N, vector<edge<int>>());
  rep(i, N-1){
    int a,b,c; cin >>a >>b >>c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  dp.assign(N, vector<vector<ll>>(2, vector<ll>(M+1)));
  // dp[i][j] := 頂点iからその部分木へ行って戻ってきて、時間jで取ってこれる税収の最大値
}

void dfs(int v, int p){
  for (auto pe : G[v]){
    int nv = pe.to;
    if (nv == p) continue;
    dfs(nv, v);
  }
  // 子の情報は全てdpに入っているとする
  dp[v][0][0] = U[v];
  dp[v][1][0] = U[v];
  // 時間0なら自分のやつは取って来れる
  for (auto pe : G[v]){
    int nv = pe.to;
    if (nv == p) continue;
    // dp[nv][j]を見ていく感じ
    // 後ろから見ていかないと更新が被りそう
    for (int j=M-pe.cost*2; j>=0; j--){
      int i = j + pe.cost * 2;
      for (int k=M-i; k>=0; k--){
        chmax(dp[v][1][k+j+pe.cost*2], dp[v][0][k] + dp[nv][0][j]);
      }
    }
    dp[v][0] = dp[v][1];
  }
}

int main() {
  input();
  dfs(0, -1);
  ll res = 0;
  for (int i=0; i<=M; i++) chmax(res, dp[0][0][i]);
  cout <<res <<endl;
}
0