結果

問題 No.417 チューリップバブル
ユーザー kura197
提出日時 2021-12-31 23:03:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 905 ms / 2,000 ms
コード長 1,335 bytes
コンパイル時間 2,229 ms
コンパイル使用メモリ 201,108 KB
最終ジャッジ日時 2025-01-27 08:10:16
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;

using P = pair<int, ll>;
int N, M;
vector<ll> U;
vector<P> G[210];

void dfs(int v, int p, vector<vector<ll>>& dp){
    REP(m, M+1) dp[v][m] = U[v];
    for(auto& [nv, c] : G[v]){
        if(nv == p) continue;
        dfs(nv, v, dp);
        for(int m0 = M; m0 >= 0; m0--){
            for(int m1 = M; m1 >= 0; m1--){
                if(m0 + m1 + 2*c <= M)
                    chmax(dp[v][m0 + m1 + 2*c], dp[v][m0] + dp[nv][m1]);
            }
        }
    }
}

int main(){
    cin >> N >> M;
    U.resize(N);
    for(auto& u : U) cin >> u;
    REP(i,N-1){
        ll a, b, c;
        cin >> a >> b >> c;
        G[a].emplace_back(b, c);
        G[b].emplace_back(a, c);
    }

    vector<vector<ll>> dp(N, vector<ll>(M+1));
    dfs(0, -1, dp);

    ll ans = 0;
    REP(m,M+1) chmax(ans, dp[0][m]);
    cout << ans << endl;
    return 0;
}
0