# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
340153 | tengiz05 | Biochips (IZhO12_biochips) | C++17 | 395 ms | 403372 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>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
template<class T> bool chmin(T& a, const T& b){return a>b?a=b,true:false;}
template<class T> bool chmax(T& a, const T& b){return a<b?a=b,true:false;}
const int N = 2e5+5, M = 505;
const int inf = 1e9;
int n, m;
int dp[N][M], w[N];
vector<int> edges[N];
int timer = 1;
int v[N], l[N];
void dfs(int u){
int tmp = timer;
for(auto v : edges[u])
dfs(v);
v[timer] = w[u];
l[timer] = tmp;
timer++;
}
void Solve(){
cin >> n >> m;
int root;
for(int i=1;i<=n;i++){
int j, cost;
cin >> j >> cost;
if(j > 0)edges[j].push_back(i);
else root = i;
w[i] = cost;
}
dfs(root);
for(int i=0;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = -inf;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = max(dp[i-1][j], dp[l[i]-1][j-1]+v[i]);
}
}
cout << dp[n][m] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie();
int t=1;
while(t--)Solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |