152142025-02-16 20:44:53antiTevefarmcpp17Runtime error 0/501ms512 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <fstream>

using namespace std;

vector<vector<int>> children;
bool selected[100000] = {0};
int db = 0;

int dfs( int u, int T[] ){
    int sum = 0;
    for(int v : children[u]){
        sum += dfs( v, T);
    }
    if(sum < T[u]){
        for(int i : children[u]){
            selected[i] = 0;
            db--;
        }
        selected[u] = 1;
        db++;
    }
    return max( sum, T[u]);

}

int main(){
    ifstream fin("be2.txt");
    int n;
    fin >> n;

    int T[n];
    for(int i=0; i<n; i++){
        fin >> T[i];
    }

    children.resize(n);
    for(int i=1; i<n; i++){
        int parent;
        fin >> parent;
        children[parent - 1].push_back(i);
    }
    cout << dfs( 0, T ) << endl;
    cout << db << endl;
    for(int i=0; i<n; i++){
        if(selected[i] == 1){
            cout << i+1 << " ";
        }
    }

}
SubtaskSumTestVerdictTimeMemory
base0/50
1Runtime error0/01ms316 KiB
2Runtime error0/01ms316 KiB
3Runtime error0/41ms316 KiB
4Runtime error0/41ms500 KiB
5Runtime error0/41ms512 KiB
6Runtime error0/41ms316 KiB
7Runtime error0/41ms316 KiB
8Runtime error0/61ms316 KiB
9Runtime error0/61ms508 KiB
10Runtime error0/61ms316 KiB
11Runtime error0/61ms316 KiB
12Runtime error0/61ms316 KiB