#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;
#define N 200010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mod 1000000007
#define pii pair <ll, ll>
#define sz(a) int(a.size())
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
ll bigmod(ll a,ll e) {if(e==0)return 1;ll x=bigmod(a*a%mod,e>>1);return e&1?x*a%mod:x;}
int n, m;
int x, TIM;
vector <int> E[N];
int id[N], go[N], v[N], dp[5][N], cost[N];
void dfs(int nd)
{
int tm = ++TIM;
id[tm] = nd;
for(auto i: E[nd]) dfs(i);
go[tm] = TIM;
}
int main()
{
cin >> n >> m;
int root = 0;
for(int i = 1; i <= n; i++) {
cin >> x >> v[i];
if(x) E[x].pb(i);
else root = i;
}
dfs(root);
for(int i = 1; i <= n; i++) dp[0][i] = v[id[i]];
int tp = 0;
for(int i = 2; i <= m; i++) {
tp = !tp;
cost[n+1] = -1e9;
for(int h = n; h >= 1; h--) cost[h] = max(cost[h+1], dp[!tp][h]);
for(int h = n; h >= 1; h--) dp[tp][h] = cost[go[h]+1] + v[id[h]];
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[tp][i]);
cout << ans;
}
Compilation message
biochips.cpp:22: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
22 | #pragma GCC optimization ("O3")
|
biochips.cpp:23: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
23 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
16 ms |
5504 KB |
Output is correct |
5 |
Correct |
17 ms |
5504 KB |
Output is correct |
6 |
Correct |
19 ms |
5504 KB |
Output is correct |
7 |
Correct |
423 ms |
11252 KB |
Output is correct |
8 |
Correct |
426 ms |
11128 KB |
Output is correct |
9 |
Correct |
530 ms |
11708 KB |
Output is correct |
10 |
Correct |
699 ms |
12280 KB |
Output is correct |