結果
| 問題 | No.1488 Max Score of the Tree |
| コンテスト | |
| ユーザー |
たたき@競プロ
|
| 提出日時 | 2023-09-03 15:20:18 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 12 ms / 2,000 ms |
| コード長 | 990 bytes |
| コンパイル時間 | 5,148 ms |
| コンパイル使用メモリ | 313,256 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-12 21:03:26 |
| 合計ジャッジ時間 | 6,405 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint=modint998244353; //1000000007;
using ll=long long;
using pp=pair<int,int>;
#define sr string
#define vc vector
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define pque priority_queue
#define bpc(a) __builtin_popcount(a)
int main(){
int n,k;cin>>n>>k;
vc v(n,vc<pp>(0));
rep(i,n-1){
int a,b,c;cin>>a>>b>>c; a--;b--;
v[a].pb({b,c}); v[b].pb({a,c});
}
int ans=0,ansx=0;
vc<pp>x;
auto f=[&](auto f,int a,int p)->int{
int res=0;
if(v[a].size()==1)res=1;
for(auto [au,c]:v[a])if(au!=p){
int t=f(f,au,a);
x.pb({c,c*t});
ansx+=c*t;
res+=t;
}
return res;
};
f(f,0,-1);
vc<int>dp(k+1,0);
for(auto [a,b]:x){
auto pre=dp;
rep(i,k)if(i+a<=k)dp[i+a]=max(dp[i+a],pre[i]+b);
}
rep(i,k+1)ans=max(ans,dp[i]);
cout<<ans+ansx;
}
たたき@競プロ