# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
673299 | 2022-12-20T06:16:26 Z | Baytoro | 바이오칩 (IZhO12_biochips) | C++17 | 503 ms | 408768 KB |
#include <bits/stdc++.h> using namespace std; #define ios ios::sync_with_stdio(false); cin.tie(NULL); #define pb push_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define fr first #define sc second //#define int long long #define endl '\n' #define cnt(x) __builtin_popcount(x) void fopn(string name){ freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout); } const int INF=1e18,mod=998244353; int n,m; const int N=2e5+5; int a[N],sz[N]; int dp[N][505]; int tmp[505]; vector<int> g[N]; void dfs(int x, int p){ sz[x]=1; dp[x][0]=0; for(auto it: g[x]){ if(it==p) continue; dfs(it,x); fill(tmp,tmp+m+1,0); for(int i=0;i<=min(sz[x],m);i++) for(int j=0;j<=min(m-i,sz[it]);j++) tmp[i+j]=max(tmp[i+j],dp[x][i]+dp[it][j]); sz[x]+=sz[it]; for(int i=1;i<=min(m,sz[x]);i++) dp[x][i]=tmp[i]; } dp[x][1]=max(dp[x][1],a[x]); } void solve(){ cin>>n>>m; int r=0; for(int i=1;i<=n;i++){ int p;cin>>p>>a[i]; if(!p)r=i; else{ g[p].pb(i); g[i].pb(p); } } dfs(r,r); cout<<dp[r][m]<<endl; } main(){ //fopn("newbarn"); ios; int T=1; //cin>>T; while(T--){ solve(); } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 4948 KB | Output is correct |
2 | Correct | 3 ms | 5028 KB | Output is correct |
3 | Correct | 3 ms | 5204 KB | Output is correct |
4 | Correct | 13 ms | 22868 KB | Output is correct |
5 | Correct | 14 ms | 25136 KB | Output is correct |
6 | Correct | 17 ms | 25180 KB | Output is correct |
7 | Correct | 283 ms | 304716 KB | Output is correct |
8 | Correct | 265 ms | 304780 KB | Output is correct |
9 | Correct | 416 ms | 370360 KB | Output is correct |
10 | Correct | 503 ms | 408768 KB | Output is correct |