9222022-01-28 12:55:34Valaki2Tevefarmcpp14Accepted 50/5072ms20928 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n;
vector<int> camel;
vector<int> parent;
vector<vector<int> > children;

pair<int, vector<int> > dfs(int cur) {
    int sum = 0;
    vector<int> merged;
    for(int nei : children[cur]) {
        pair<int, vector<int> > cur_res = dfs(nei);
        sum += cur_res.fi;
        for(int x : cur_res.se) {
            merged.pb(x);
        }
    }
    if(camel[cur] > sum) {
        return mp(camel[cur], vector<int> (1, cur));
    } else {
        return mp(sum, merged);
    }
}

void solve() {
    cin >> n;
    camel.assign(1 + n, 0);
    parent.assign(1 + n, 0);
    children.assign(1 + n, vector<int> (0, 0));
    for(int i = 1; i <= n; i++) {
        cin >> camel[i];
    }
    for(int i = 2; i <= n; i++) {
        cin >> parent[i];
        children[parent[i]].pb(i);
    }
    pair<int, vector<int> > answer = dfs(1);
    cout << answer.fi << "\n";
    cout << answer.se.size() << "\n";
    for(int x : answer.se) {
        cout << x << " ";
    }
    cout << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1820 KiB
2Accepted0/02ms2016 KiB
3Accepted4/41ms1872 KiB
4Accepted4/41ms1900 KiB
5Accepted4/41ms1924 KiB
6Accepted4/42ms1968 KiB
7Accepted4/430ms8888 KiB
8Accepted6/637ms11016 KiB
9Accepted6/648ms13360 KiB
10Accepted6/656ms15436 KiB
11Accepted6/664ms17816 KiB
12Accepted6/672ms20928 KiB