15402022-11-22 18:26:27TomaSajtTevefarmcpp14Accepted 50/5046ms14452 KiB
#include <bits/stdc++.h>
#define speed ios::sync_with_stdio(0);cin.tie(0)
using namespace std;

vector<vector<int>> g;
vector<int> supply;
vector<bool> marked;
vector<long long> tot;
vector<int> res;

void dfs1(int u) {
    if (g[u].empty()) {
        marked[u] = 1;
        tot[u] = supply[u];
        return;
    }
    tot[u] = 0;
    for (auto v : g[u]) {
        dfs1(v);
        tot[u] += tot[v];
    }
    if (tot[u] < supply[u]) {
        marked[u] = 1;
        tot[u] = supply[u];
    }
}

void dfs2(int u) {
    if (marked[u]) {
        res.push_back(u);
        return;
    }
    for (auto v : g[u]) dfs2(v);
}

int main() {
    speed;
    int n;
    cin >> n;
    g.resize(n + 1);
    marked.resize(n + 1);
    tot.resize(n + 1);
    supply.resize(n + 1);
    for (int i = 1; i <= n; i++) cin >> supply[i];
    for (int i = 2; i <= n; i++) {
        int a; cin >> a;
        g[a].push_back(i);
    }
    dfs1(1);
    dfs2(1);
    cout << tot[1] << '\n' << res.size() << '\n';
    for (auto u : res) cout << u << ' ';

}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1896 KiB
2Accepted0/03ms2308 KiB
3Accepted4/42ms2316 KiB
4Accepted4/42ms2496 KiB
5Accepted4/42ms2852 KiB
6Accepted4/42ms3088 KiB
7Accepted4/421ms8704 KiB
8Accepted6/626ms10172 KiB
9Accepted6/630ms11328 KiB
10Accepted6/634ms12428 KiB
11Accepted6/641ms13420 KiB
12Accepted6/646ms14452 KiB