46862023-03-31 08:54:30Albertstein99Tevefarmcpp17Wrong answer 38/5090ms11660 KiB
#include <bits/stdc++.h>
using namespace std;

vector <vector <int> > g;
vector <int> W;
vector <bool> Q;
vector <int> ans;


int dfs(int node){
    int maxi = 0;
    for(auto x : g[node])
        maxi += dfs(x);
    if(maxi > W[node])
        return maxi;
    Q[node] = 1;
    return W[node];
}

void dfs2(int node){
    if(Q[node]){
        ans.push_back(node);
        return;
    }
    for(auto x : g[node])
        dfs2(x);
}

int main(){
    int N;
    cin >> N;
    W.resize(N + 1);
    g.resize(N + 1);
    Q.resize(N + 1);
    for(int i = 1; i <= N; i++)
        cin >> W[i];
    for(int i = 2; i <= N; i++){
        int x;
        cin >> x;
        g[x].push_back(i);
    }
    cout << dfs(1) << '\n';
    dfs2(1);
    cout << ans.size() << '\n';
    for(auto x : ans){
        cout << x << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base38/50
1Accepted0/03ms1744 KiB
2Accepted0/03ms2088 KiB
3Accepted4/42ms2128 KiB
4Accepted4/43ms2340 KiB
5Accepted4/43ms2440 KiB
6Accepted4/43ms2596 KiB
7Accepted4/441ms7524 KiB
8Accepted6/652ms8616 KiB
9Accepted6/659ms9872 KiB
10Accepted6/667ms10804 KiB
11Wrong answer0/682ms10712 KiB
12Wrong answer0/690ms11660 KiB