답안 #523699

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523699 2022-02-08T04:29:44 Z knightron0 바이오칩 (IZhO12_biochips) C++17
100 / 100
167 ms 16268 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double

const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 2e5 + 5;

vector<int> adj[MAXN];
int val[MAXN], timer = 0, sz[MAXN], arr[MAXN];

int dfs(int s){
    int curr = ++timer;
    arr[curr] = val[s];
    sz[curr] = 1;
    for(auto u: adj[s]){
        sz[curr] += dfs(u);
    }
    return sz[curr];
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif
    int n, m;
    cin>>n>>m;
    int root = -1;
    for(int i= 1;i<=n;i++){
        int t1, t2;
        cin>>t1>>t2;
        val[i] = t2;
        if(t1 == 0){
            root = i;
            continue;
        } else {
            adj[t1].pb(i);
        }
    }
    dfs(root);
    int dp[n+3][2];
    for(int i =1;i<=n+1;i++){
        dp[i][0] = 0;
    }
    for(int i =1;i<=m;i++){
        dp[n+1][i%2] = -INF;
        for(int j= n;j>=1;j--){
            dp[j][i%2] = max(dp[j+1][i%2], dp[j+sz[j]][(i-1)%2] + arr[j]);
        }
    }
    cout<<dp[1][m%2]<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 8 ms 5516 KB Output is correct
5 Correct 9 ms 5496 KB Output is correct
6 Correct 7 ms 5580 KB Output is correct
7 Correct 89 ms 12944 KB Output is correct
8 Correct 94 ms 13068 KB Output is correct
9 Correct 136 ms 15456 KB Output is correct
10 Correct 167 ms 16268 KB Output is correct