답안 #13288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
13288 2015-02-10T08:17:22 Z gs14004 바이오칩 (IZhO12_biochips) C++14
100 / 100
311 ms 403844 KB
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int n,m,inp[200005];

int piv,sz[200005],a[200005];
vector<int> g[200005];
int root;

int dfs(int x){
    int num = ++piv;
    a[num] = inp[x];
    sz[num] = 1;
    for(int &i : g[x]){
        sz[num] += dfs(i);
    }
    return sz[num];
}

int dp[505][200005];

int main(){
    scanf("%d %d",&n,&m);
    for (int i=1; i<=n; i++) {
        int p;
        scanf("%d %d",&p,&inp[i]);
        if(p) g[p].push_back(i);
        else root = i;
    }
    dfs(root);
    for (int i=1; i<=m; i++) {
        dp[i][n+1] = -1e9;
        for (int j=n; j; j--) {
            dp[i][j] = max(dp[i][j+1],dp[(i-1)][j+sz[j]] + a[j]);
        }
    }
    printf("%d",dp[m][1]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 402788 KB Output is correct
2 Correct 0 ms 402788 KB Output is correct
3 Correct 2 ms 402788 KB Output is correct
4 Correct 9 ms 402920 KB Output is correct
5 Correct 8 ms 402788 KB Output is correct
6 Correct 3 ms 402788 KB Output is correct
7 Correct 177 ms 403844 KB Output is correct
8 Correct 156 ms 403844 KB Output is correct
9 Correct 247 ms 403712 KB Output is correct
10 Correct 311 ms 403712 KB Output is correct