18252022-12-04 13:42:08kdbTevefarmcpp11Accepted 50/5032ms8228 KiB
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

ostream& operator<<(ostream& os, vector<int>& input)
{
    for (auto i : input)
    {
        os << i << " ";
    }
    return os;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    vector<int>a(100001);
    vector<ll>t(100001);
    vector<ll>tc(100001);
    vector<bool>benneVanE(100001);
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) cin >> t[i];
    for (int i = 2; i <= n; i++) cin >> a[i];

    ll ans = 0;
    for (int i = n; i >= 1; i--) {
        if (t[i] > tc[i]) {
            ans -= tc[i];
            ans += t[i];
            benneVanE[i] = true;
            tc[a[i]] += t[i];
        }
        else {
            tc[a[i]] += tc[i];
        }
    }
    cout << ans << endl;
    vector<int> ansv;
    for (int i = 1; i <= n; i++)
    {
        if (benneVanE[i] && !benneVanE[a[i]]) ansv.push_back(i);
        benneVanE[i] = benneVanE[i] || benneVanE[a[i]];
    }

    cout << ansv.size() << endl << ansv << endl;
    return 0;
}
/*
8
5 5 7 1 2 3 2 4
1
1
2
2
2
3
3
-13
-4
-3 4 5 6
*/
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms5700 KiB
2Accepted0/04ms5992 KiB
3Accepted4/44ms6200 KiB
4Accepted4/43ms6540 KiB
5Accepted4/43ms6388 KiB
6Accepted4/44ms6680 KiB
7Accepted4/416ms7340 KiB
8Accepted6/618ms7552 KiB
9Accepted6/621ms7908 KiB
10Accepted6/624ms8068 KiB
11Accepted6/628ms8032 KiB
12Accepted6/632ms8228 KiB