p195.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<vector<int>> adj;

void read() {
    cin >> n;
    adj.assign(n, {});
    for(int i = 1; i < n; i++) {
        int p;
        cin >> p;
        adj[p - 1].push_back(i);
    }
}

void solve() {
    vector<vector<int>> dp(n, vector<int>(2, 0));
    for(int u = n - 1; u >= 0; u--) {
        dp[u][1] = 1;
        for(int v: adj[u]) {
            dp[u][1] += dp[v][0];
        }

        int sum_dp_0 = 0;
        for(int v: adj[u]) {
            sum_dp_0 += dp[v][0];
        }

        for(int v: adj[u]) {
            dp[u][0] = max(dp[u][0], sum_dp_0 - dp[v][0] + dp[v][1]);
        }
        dp[u][1] = max(dp[u][0], dp[u][1]);
    }

    vector<int> ans;
    vector<bool> take(n, false);
    for(int u = 0; u < n; u++) {
        if(take[u] && dp[u][1] == dp[u][0]) {
            take[u] = false;
        }

        if(take[u]) {
            ans.push_back(u + 1);
            for(int v: adj[u]) {
                take[v] = false;
            }
        } else {
            int sum_dp_0 = 0;
            for(int v: adj[u]) {
                sum_dp_0 += dp[v][0];
            }

            int take_child = -1;
            for(int v: adj[u]) {
                if(dp[u][0] == sum_dp_0 - dp[v][0] + dp[v][1]) {
                    take_child = v;
                    take[v] = true;
                    break;
                }
            }

            for(int v: adj[u]) {
                if(v != take_child) {
                    take[v] = false;
                }
            }
        }
    }

    cout << dp[0][0] * 1000 << '\n';
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
195. New Year Bonus Grant
time limit per test: 0.75 sec.
memory limit per test: 65536 KB
input: standard
output: standard



All programmers of Mocrosoft software company are organized in a strict subordination hierarchy. Every programmer has exactly one chief, except Bill Hates who is also the head of the company and has no chief.

Due to the celebration of the new 2003 year, chief accountant of Mocrosoft decided to pay a New Year Bonus Grant of 1000 dollars to some programmers. However being extremely concerned of the company wealth she would like to designate the least possible amount of money for these grants. On the other hand she didn't want to be accused of being too greedy or of giving preferences to some programmers. To do this, she developed the following scheme of grants appointment:


Each programmer may either assign a grant to one of his subordinates or have a grant assigned to him by his chief or none of the above.
No programmer can simultaneously receive a grant and assign a grant to one of his subordinates.
No programmer can assign a grant to more than one of his subordinates


The scheme seemed to be designed perfectly — nobody would like to assign a grant to anybody since in this case he himself would not receive money. But programmers somehow discovered the plan of chief accountant and decided to make a trick to get the most money possible and share them fairly afterwards. The idea was to make such grant assignments that the total amount of grant money received is maximum possible.

You were selected to write the program which will find the optimal grants appointment.

Input

The first line of the input file contains integer N — the number of programmers in Mocrosoft company (2 ≤ N ≤ 500 000). Each programmer is assigned his unique identifier — integer number ranging from 1 to N. Bill Hates has number 1 and each programmer has the number greater then the number of his chief. The second line of the input file contains N-1 integers, i-th of which being the number of the chief of the worker whose number is (i + 1).

Output

On the first line of the output file print the maximum possible amount of money workers can get. On the second line output the numbers of programmers that will receive grant in ascending order.

Sample test(s)

Input
4
1 1 2

Output
2000
3 4

Author:	Andrew Stankevich
Resource:	Petrozavodsk Winter Trainings 2003
Date:	2003-02-06






=================
