CSES - Aalto Competitive Programming 2024 - wk6 - Mon - Results
Submission details
Task:Tree game
Sender:AleksandrPolitov
Submission time:2024-10-07 17:37:14 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.01 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.01 sdetails
#15ACCEPTED0.01 sdetails
#16ACCEPTED0.01 sdetails
#17ACCEPTED0.01 sdetails
#18ACCEPTED0.01 sdetails
#19ACCEPTED0.01 sdetails
#20ACCEPTED0.01 sdetails
#21ACCEPTED0.01 sdetails
#22ACCEPTED0.01 sdetails
#23ACCEPTED0.01 sdetails
#24ACCEPTED0.01 sdetails
#25ACCEPTED0.01 sdetails
#26ACCEPTED0.01 sdetails
#27ACCEPTED0.01 sdetails
#28ACCEPTED0.01 sdetails
#29ACCEPTED0.01 sdetails
#30ACCEPTED0.01 sdetails
#31ACCEPTED0.01 sdetails
#32ACCEPTED0.01 sdetails
#33ACCEPTED0.01 sdetails
#34ACCEPTED0.01 sdetails
#35ACCEPTED0.01 sdetails
#36ACCEPTED0.01 sdetails
#37ACCEPTED0.01 sdetails
#38ACCEPTED0.01 sdetails
#39ACCEPTED0.01 sdetails
#40ACCEPTED0.01 sdetails
#41ACCEPTED0.01 sdetails
#42ACCEPTED0.01 sdetails
#43ACCEPTED0.01 sdetails
#44ACCEPTED0.01 sdetails
#45ACCEPTED0.01 sdetails
#46ACCEPTED0.01 sdetails
#47ACCEPTED0.01 sdetails
#48ACCEPTED0.01 sdetails
#49ACCEPTED0.01 sdetails
#50ACCEPTED0.01 sdetails
#51ACCEPTED0.01 sdetails
#52ACCEPTED0.01 sdetails
#53ACCEPTED0.01 sdetails
#54ACCEPTED0.01 sdetails
#55ACCEPTED0.01 sdetails
#56ACCEPTED0.01 sdetails
#57ACCEPTED0.01 sdetails
#58ACCEPTED0.01 sdetails
#59ACCEPTED0.01 sdetails
#60ACCEPTED0.05 sdetails
#61ACCEPTED0.07 sdetails
#62ACCEPTED0.06 sdetails
#63ACCEPTED0.05 sdetails
#64ACCEPTED0.06 sdetails
#65ACCEPTED0.07 sdetails
#66ACCEPTED0.06 sdetails
#67ACCEPTED0.06 sdetails
#68ACCEPTED0.05 sdetails
#69ACCEPTED0.06 sdetails

Code

#ifdef ONPC
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#define char unsigned char
#define rep(i, a, b) for(int i=a; i< (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back

#define LSOne(S) ((S) & -(S))

using namespace std;
// mt19937 rnd(239);
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> int sgn(T x) { return (T(0) < x) - (x < T(0)); }
typedef long double T;
typedef complex<T> pt;
#define x real()
#define y imag()

template<class T>
istream& operator>> (istream& is, complex<T>& p) {
    T value;
    is >> value;
    p.real(value);
    is >> value;
    p.imag(value);
    return is;
}

typedef long long ll;
typedef long double ld;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif

template<typename S, typename T = S> void chmin(S &s, T t) {s = s < t ? s : t;}
template<typename S, typename T = S> void chmax(S &s, T t) {s = s > t ? s : t;}

const int INF = 1e9; // 10^9 = 1B is < 2^31-1
const ll LLINF = 4e18; // 4*10^18 is < 2^63-1
const double EPS = 1e-9;
const ll MOD = 1e9+7;


const int N=1e5+10;
//ll p[N];
vector<int> adj[N];
ll a[N];
ll dp[N];



void dfs(int node) {
    dp[node]=a[node];
    vector<pair<ll,ll>> poss;
    for(auto &w:adj[node]) {
        dfs(w);
        poss.pb({dp[w], w});
    }
    sort(poss.rbegin(), poss.rend());
    /*
    std::cout << node;
    std::cout << ": ";
    for (int i = 0; i < poss.size(); i++) {
        std::cout << poss[i].fi << "(" << poss[i].se << ")"  << " ";
    }
    std::cout  << std::endl;
    */
    if(poss.size()<=1) return;
    else {
        dp[node]+=poss[1].fi;
    }
    //__print(poss); std::cout  << std::endl;
}

int solve() {
    int n; std::cin >> n;

    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }

    for (int i = 1; i <= n; i++) {
        int P; std::cin >> P;
        if(i==1) continue;
        adj[P].pb(i);
    }


    dfs(1);
    //for (int i = 1; i <= n; i++) {
        //std::cout << dp[i] << " ";
    //}
    //std::cout  << std::endl;
    

    std::cout << dp[1] << std::endl;

    //std::cout << dfs2(1) << std::endl;



    return 0;
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int TET = 1;
    //cin >> TET;
    for (int i = 1; i <= TET; i++) {
        #ifdef ONPC
            cout << "TEST CASE#" << i << endl;
        #endif
        
        if (solve()) {
            break;
        }

        #ifdef ONPC
            cout << "__________________________" << endl;
        #endif
    }
    #ifdef ONPC
        cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
    #endif
}

Test details

Test 1

Verdict: ACCEPTED

input
1


correct output
8

user output
8

Test 2

Verdict: ACCEPTED

input
2
8 8 
1 1 

correct output
8

user output
8

Test 3

Verdict: ACCEPTED

input
3
3 1 6 
1 3 1 

correct output
3

user output
3

Test 4

Verdict: ACCEPTED

input
3
5 8 4 
1 1 2 

correct output
5

user output
5

Test 5

Verdict: ACCEPTED

input
3
8 8 3 
1 1 1 

correct output
11

user output
11

Test 6

Verdict: ACCEPTED

input
4
9 8 1 6 
1 1 2 2 

correct output
9

user output
9

Test 7

Verdict: ACCEPTED

input
4
10 1 4 3 
1 1 1 2 

correct output
11

user output
11

Test 8

Verdict: ACCEPTED

input
4
9 10 2 6 
1 4 1 1 

correct output
11

user output
11

Test 9

Verdict: ACCEPTED

input
4
7 4 9 2 
1 1 1 3 

correct output
11

user output
11

Test 10

Verdict: ACCEPTED

input
5
9 5 7 7 4 
1 1 4 2 4 

correct output
9

user output
9

Test 11

Verdict: ACCEPTED

input
5
10 2 3 1 4 
1 1 1 1 3 

correct output
12

user output
12

Test 12

Verdict: ACCEPTED

input
5
5 5 4 4 2 
1 4 4 1 2 

correct output
5

user output
5

Test 13

Verdict: ACCEPTED

input
5
6 9 5 9 1 
1 1 1 3 1 

correct output
11

user output
11

Test 14

Verdict: ACCEPTED

input
5
7 7 6 3 2 
1 1 2 3 3 

correct output
7

user output
7

Test 15

Verdict: ACCEPTED

input
5
10 5 1 7 4 
1 1 5 3 1 

correct output
14

user output
14

Test 16

Verdict: ACCEPTED

input
5
4 2 10 6 1 
1 1 2 1 1 

correct output
6

user output
6

Test 17

Verdict: ACCEPTED

input
5
5 10 4 6 3 
1 1 2 1 2 

correct output
11

user output
11

Test 18

Verdict: ACCEPTED

input
5
9 3 5 1 7 
1 5 5 3 1 

correct output
9

user output
9

Test 19

Verdict: ACCEPTED

input
5
4 2 1 3 9 
1 1 1 1 1 

correct output
7

user output
7

Test 20

Verdict: ACCEPTED

input
10
9 1 10 3 4 5 8 9 6 5 
1 4 2 1 7 4 8 6 7 8 

correct output
9

user output
9

Test 21

Verdict: ACCEPTED

input
10
4 7 4 10 6 9 5 4 7 6 
1 1 1 2 1 1 8 2 3 3 

correct output
14

user output
14

Test 22

Verdict: ACCEPTED

input
10
7 2 3 5 3 7 7 9 6 7 
1 8 2 7 8 7 1 1 6 7 

correct output
18

user output
18

Test 23

Verdict: ACCEPTED

input
10
3 3 1 1 5 7 1 2 5 5 
1 5 7 2 1 5 8 5 1 1 

correct output
8

user output
8

Test 24

Verdict: ACCEPTED

input
10
1 7 3 10 5 7 8 1 2 6 
1 1 1 2 9 2 9 3 2 9 

correct output
4

user output
4

Test 25

Verdict: ACCEPTED

input
10
6 5 3 10 2 9 1 7 8 9 
1 8 5 1 1 10 2 5 8 2 

correct output
11

user output
11

Test 26

Verdict: ACCEPTED

input
10
5 6 4 5 7 9 5 8 8 4 
1 1 4 2 6 1 6 5 1 1 

correct output
13

user output
13

Test 27

Verdict: ACCEPTED

input
10
1 5 3 1 5 6 7 9 9 4 
1 5 10 9 1 9 9 4 5 5 

correct output
1

user output
1

Test 28

Verdict: ACCEPTED

input
10
5 5 6 9 5 10 6 2 6 8 
1 1 4 9 9 1 2 1 6 8 

correct output
10

user output
10

Test 29

Verdict: ACCEPTED

input
10
3 2 1 4 4 5 2 4 9 4 
1 5 1 2 1 1 3 1 1 9 

correct output
8

user output
8

Test 30

Verdict: ACCEPTED

input
100
21461600 42077569 474283491 30...

correct output
966865392

user output
966865392

Test 31

Verdict: ACCEPTED

input
100
578129114 177547714 700400588 ...

correct output
1855515684

user output
1855515684

Test 32

Verdict: ACCEPTED

input
100
994871996 791076134 5953277 40...

correct output
1814515836

user output
1814515836

Test 33

Verdict: ACCEPTED

input
100
222228197 71907871 993998009 7...

correct output
1135951805

user output
1135951805

Test 34

Verdict: ACCEPTED

input
100
579900832 174882180 138713252 ...

correct output
716202927

user output
716202927

Test 35

Verdict: ACCEPTED

input
100
189754281 378059061 342381828 ...

correct output
1551273774

user output
1551273774

Test 36

Verdict: ACCEPTED

input
100
915344 434975452 466660908 159...

correct output
907612293

user output
907612293

Test 37

Verdict: ACCEPTED

input
100
857806942 521650866 432061350 ...

correct output
2316691892

user output
2316691892

Test 38

Verdict: ACCEPTED

input
100
126184681 630426411 840357157 ...

correct output
126184681

user output
126184681

Test 39

Verdict: ACCEPTED

input
100
877496128 782849725 556141400 ...

correct output
2248009755

user output
2248009755

Test 40

Verdict: ACCEPTED

input
200
903167338 160468896 971358120 ...

correct output
2615788195

user output
2615788195

Test 41

Verdict: ACCEPTED

input
200
139339061 77281799 316066077 3...

correct output
729411647

user output
729411647

Test 42

Verdict: ACCEPTED

input
200
19617302 94019529 17298266 328...

correct output
983806343

user output
983806343

Test 43

Verdict: ACCEPTED

input
200
578170987 336960706 697473976 ...

correct output
1901938137

user output
1901938137

Test 44

Verdict: ACCEPTED

input
200
956690801 79083637 339599313 1...

correct output
1857249559

user output
1857249559

Test 45

Verdict: ACCEPTED

input
200
185716349 143762158 115416001 ...

correct output
1057534782

user output
1057534782

Test 46

Verdict: ACCEPTED

input
200
275288896 20815354 254338864 6...

correct output
1267445164

user output
1267445164

Test 47

Verdict: ACCEPTED

input
200
24900465 555962124 563413635 3...

correct output
747057370

user output
747057370

Test 48

Verdict: ACCEPTED

input
200
513524772 254574588 366558027 ...

correct output
2272884295

user output
2272884295

Test 49

Verdict: ACCEPTED

input
200
490774594 733870697 844510835 ...

correct output
1650288517

user output
1650288517

Test 50

Verdict: ACCEPTED

input
1000
861884121 322555995 898039039 ...

correct output
2972059986

user output
2972059986

Test 51

Verdict: ACCEPTED

input
1000
659913933 513100418 571680180 ...

correct output
659913933

user output
659913933

Test 52

Verdict: ACCEPTED

input
1000
209874718 348676066 464074417 ...

correct output
899398937

user output
899398937

Test 53

Verdict: ACCEPTED

input
1000
645792798 158522181 659799184 ...

correct output
2154979899

user output
2154979899

Test 54

Verdict: ACCEPTED

input
1000
859348061 421675209 599301462 ...

correct output
2387245650

user output
2387245650

Test 55

Verdict: ACCEPTED

input
1000
226879800 528482225 208763773 ...

correct output
1424568237

user output
1424568237

Test 56

Verdict: ACCEPTED

input
1000
233113004 975537019 524093912 ...

correct output
1182124106

user output
1182124106

Test 57

Verdict: ACCEPTED

input
1000
874049734 75947226 679836952 6...

correct output
2874667578

user output
2874667578

Test 58

Verdict: ACCEPTED

input
1000
136403860 348337953 506386641 ...

correct output
136403860

user output
136403860

Test 59

Verdict: ACCEPTED

input
1000
824089343 410028787 814008876 ...

correct output
1778275291

user output
1778275291

Test 60

Verdict: ACCEPTED

input
100000
106979616 574727912 291579092 ...

correct output
3411435693

user output
3411435693

Test 61

Verdict: ACCEPTED

input
100000
834972396 341945130 512367317 ...

correct output
1494272515

user output
1494272515

Test 62

Verdict: ACCEPTED

input
100000
964109644 356056141 521072685 ...

correct output
1283227450

user output
1283227450

Test 63

Verdict: ACCEPTED

input
100000
779580014 612148564 793692717 ...

correct output
4717954657

user output
4717954657

Test 64

Verdict: ACCEPTED

input
100000
124576659 286156851 307400435 ...

correct output
124576659

user output
124576659

Test 65

Verdict: ACCEPTED

input
100000
452020023 593221033 326082759 ...

correct output
1178920108

user output
1178920108

Test 66

Verdict: ACCEPTED

input
100000
378587281 1836202 116838722 72...

correct output
587215770

user output
587215770

Test 67

Verdict: ACCEPTED

input
100000
877997603 741843452 187099247 ...

correct output
1018714881

user output
1018714881

Test 68

Verdict: ACCEPTED

input
100000
280774790 48087557 668312713 4...

correct output
3930265099

user output
3930265099

Test 69

Verdict: ACCEPTED

input
100000
866596482 430317267 394041807 ...

correct output
1639137122

user output
1639137122