# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
519402 | tphuc2908 | Biochips (IZhO12_biochips) | C++14 | 592 ms | 408700 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define repi(i,x,y) for(int i = x; i >= y; --i)
#define ci(x) int x; cin>> x
#define TC(t) ci(t); while(t--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define cii(x, y) ci(x); ci(y)
#define ciii(x, y, z) ci(x); ci(y); ci(z)
#define mp make_pair
//#define int long long
typedef long long ll;
typedef vector<int> vi;
const int N = 2e5 + 5;
const int NN = 16 + 15;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int pi = 31;
const int inf = 1e9 + 6;
const int block = 500;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
void readfile(){
#ifdef ONLINE_JUDGE
#else
freopen("text.inp", "r", stdin);
#endif // ONLINE_JUDGE
// freopen("biochip.inp", "r", stdin);
// freopen("biochip.out", "w", stdout);
}
int n, m;
vector<int> g[N];
int val[N];
void inp(){
cin >> n >> m;
rep(i,1,n){
cii(cha, v);
g[i].pb(cha);
g[cha].pb(i);
val[i] = v;
}
}
int sz[N], dp[505], f[N][505];
void dfs(int s, int p){
sz[s] = 1;
for(int v : g[s])
if(v!=p){
dfs(v, s);
sz[s] += sz[v];
}
fill(dp + 0, dp + m + 1, 0);
int cnt = 0;
for(int v : g[s]){
if(v==p) continue;
rep(i,0,cnt)
rep(j,0,min(sz[v], m)){
if(i + j > m) break;
dp[i + j] = max(dp[i+j], f[s][i] + f[v][j]);
}
rep(i,0,min(cnt+sz[v], m))
f[s][i] = dp[i], dp[i] = 0;
cnt = min(cnt + sz[v], m);
}
f[s][1] = max(f[s][1], val[s]);
}
void process(){
dfs(0, -1);
cout << f[0][m];
}
int main() {
// readfile();
ios_base::sync_with_stdio(0);
cin.tie(0);
// while(cin >> n >> m){
inp();
process();
// reset();
// }
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |