답안 #32008

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
32008 2017-09-21T13:57:08 Z szawinis 바이오칩 (IZhO12_biochips) C++14
100 / 100
333 ms 402488 KB
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5+1;

int n, m, x[MAX], ed[MAX], dp[MAX][501];
vector<int> g[MAX], ord;

void dfs(int u) {
	ord.push_back(u);
	for(int v: g[u]) dfs(v);
	ed[u] = ord.size();
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	int root = -1;
	for(int i = 0, p; i < n; i++) {
		cin >> p >> x[i]; --p;
		if(p >= 0) g[p].push_back(i);
		else root = i;
	}
	dfs(root);
	for(int j = 1; j <= m; j++) dp[n][j] = INT_MIN;
	for(int i = n-1; i >= 0; i--) {
		for(int j = 0; j <= m; j++) {
			dp[i][j] = dp[i+1][j];
			if(j) dp[i][j] = max(dp[ed[ord[i]]][j-1] + x[ord[i]], dp[i][j]);
		}
	}
	cout << dp[0][m];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 399832 KB Output is correct
2 Correct 0 ms 399832 KB Output is correct
3 Correct 0 ms 399832 KB Output is correct
4 Correct 6 ms 400136 KB Output is correct
5 Correct 6 ms 400112 KB Output is correct
6 Correct 13 ms 400112 KB Output is correct
7 Correct 229 ms 402488 KB Output is correct
8 Correct 153 ms 402484 KB Output is correct
9 Correct 213 ms 402432 KB Output is correct
10 Correct 333 ms 402432 KB Output is correct