#include<bits/stdc++.h>
// #define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e9
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "biochips"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
int root,n,k;
vector<int> g[MAXN];
void solve(){
cin>>n>>k;
vector<int> par(n+3),etour(n+3), in(n+3), out(n+3), value(n+3);
vector<vector<int>> dp(n+3,vector<int>(k+3,0));
int id = 0;
function<void(int,int)> dfs = [&](int u,int p){
etour[++id] = u;
in[u] = id;
for(auto v: g[u]){
if (v==p) continue;
dfs(v,u);
}
out[u] = id;
};
for(int i=1;i<=n;i++){
cin>>par[i]>>value[i];
if (par[i] == 0) root = i;
else {
g[i].pb(par[i]);
g[par[i]].pb(i);
}
}
dfs(root,-1);
for(int i=1;i<=k;i++){
dp[n+1][i] = -INF;
}
dp[n+1][0] = 0;
for(int i=n;i>=1;i--){
for(int j=0;j<=k;j++){
int u = etour[i];
maximize(dp[i][j],dp[i+1][j]);
if (j>=1) maximize(dp[i][j],dp[out[u]+1][j-1] + value[u]);
}
}
cout<<dp[1][k]<<endl;
}
main()
{
fast;
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
solve();
}
Compilation message
biochips.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main()
| ^~~~
biochips.cpp: In function 'int main()':
biochips.cpp:66:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:67:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
7512 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
6 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
7 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
9 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
7768 KB |
Output isn't correct |