답안 #344772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
344772 2021-01-06T14:03:45 Z Gurban 바이오칩 (IZhO12_biochips) C++17
0 / 100
30 ms 10092 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 505;
const int maxn=200005;
int n,m,root;
int p[maxn],a[maxn],cnt;
int mx[maxn],l[maxn],r[maxn];
vector<int>E[maxn],v;
int dp[maxn][N],ps[maxn];

void dfs(int nd){
	mx[nd] = a[nd];
	if(E[nd].empty()){
		l[nd]=nd,r[nd]=nd; cnt++;
		v.push_back(nd); ps[nd] = (int)v.size()-1;
		return;
		
	}
	for(auto i : E[nd]){
		dfs(i);
		if(l[nd] == 0) l[nd] = l[i];
		r[nd] = r[i];
		mx[nd] = max(mx[nd],mx[i]);
	}
}

void f(int pos,int nd){
	if(nd == 0 or r[nd] != v[pos]) return;
	int lft = ps[l[nd]] - 1;
	for(int i = 1;i <= min(m,lft + 1);i++)
		dp[pos][i] = max(dp[lft][i-1]+mx[nd],dp[pos][i]);
	f(pos,p[nd]);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m;
	for(int i = 1;i <= n;i++){
		cin >> p[i] >> a[i];
		if(p[i] == 0) root = i;
		else E[p[i]].push_back(i);
	}
	
	v.push_back(-1);
	dfs(root);
	assert(cnt >= m);
	
	for(int i = 0;i < (int)v.size();i++) for(int j = 1;j <= m;j++) dp[i][j] = -1e9;

	for(int i = 1;i < (int)v.size();i++){
		f(i,v[i]);
		for(int j = 1;j <= m;j++) dp[i][j] = max(dp[i][j],dp[i-1][j]);
	}
	
	cout<<dp[(int)v.size()-1][m];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Runtime error 30 ms 10092 KB Execution killed with signal 6 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -