CSES - Aalto Competitive Programming 2024 - wk12 - Mon - Results
Submission details
Task:Chain Reaction
Sender:aalto2024m_004
Submission time:2024-11-25 17:41:12 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.19 sdetails
#14ACCEPTED0.14 sdetails
#15ACCEPTED0.17 sdetails
#16ACCEPTED0.19 sdetails
#17ACCEPTED0.18 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.01 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.18 sdetails
#24ACCEPTED0.18 sdetails
#25ACCEPTED0.18 sdetails
#26ACCEPTED0.18 sdetails
#27ACCEPTED0.19 sdetails
#28ACCEPTED0.19 sdetails
#29ACCEPTED0.19 sdetails
#30ACCEPTED0.18 sdetails
#31ACCEPTED0.19 sdetails
#32ACCEPTED0.19 sdetails

Code

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#include <climits>

using ll = long long int;

int main() {
    int n, s;
    cin >> n >> s;
    s--; 
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    vector<vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--; 
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    vector<ll> max_charge(n, -LLONG_MAX); 
    max_charge[s] = 1;

    queue<int> q;
    q.push(s);
    vector<bool> visited(n, false);
    visited[s] = true;

    ll global_max = 1;

    while (!q.empty()) {
        int u = q.front();
        q.pop();

        for (int v : adj[u]) {
            if (!visited[v]) {
                visited[v] = true;

                ll new_charge1 = max_charge[u] + a[v] - a[u];
                ll new_charge2 = max_charge[u] + b[v] - b[u];

                max_charge[v] = max({max_charge[v], new_charge1, new_charge2});
                global_max = max(global_max, max_charge[v]);

                q.push(v);
            }
        }
    }

    cout << global_max << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
5 1
4 2 1 1 1
5 2 1 5 4
1 2
1 3
...

correct output
2

user output
2

Test 2

Verdict: ACCEPTED

input
4 1
4 2 2 1
1 1 1 1
1 2
2 3
...

correct output
1

user output
1

Test 3

Verdict: ACCEPTED

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

correct output
19

user output
19

Test 4

Verdict: ACCEPTED

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

correct output
9

user output
9

Test 5

Verdict: ACCEPTED

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

correct output
11

user output
11

Test 6

Verdict: ACCEPTED

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

correct output
19

user output
19

Test 7

Verdict: ACCEPTED

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

correct output
15

user output
15

Test 8

Verdict: ACCEPTED

input
10 10
80 58 90 31 79 35 10 75 83 70
48 2 25 73 99 7 53 79 36 89
9 4
7 9
...

correct output
54

user output
54

Test 9

Verdict: ACCEPTED

input
10 9
50 84 12 34 86 71 73 78 6 62
29 97 88 56 84 94 94 98 36 96
5 10
3 1
...

correct output
149

user output
149

Test 10

Verdict: ACCEPTED

input
10 5
7 66 89 87 27 80 2 11 29 3
98 7 52 66 21 2 26 19 96 31
4 6
1 10
...

correct output
226

user output
226

Test 11

Verdict: ACCEPTED

input
10 3
91 13 67 44 6 83 21 8 23 14
54 19 30 26 60 73 22 90 36 84
5 10
4 2
...

correct output
151

user output
151

Test 12

Verdict: ACCEPTED

input
10 5
87 61 47 96 50 23 35 54 26 87
16 60 77 75 77 91 59 16 23 51
5 1
10 8
...

correct output
175

user output
175

Test 13

Verdict: ACCEPTED

input
97304 87743
876894525 229121152 393060078 ...

correct output
131459576065

user output
131459576065

Test 14

Verdict: ACCEPTED

input
74779 53246
763040787 482885163 932005790 ...

correct output
155373737827

user output
155373737827

Test 15

Verdict: ACCEPTED

input
89537 85789
432185633 432110306 240073971 ...

correct output
255801753825

user output
255801753825

Test 16

Verdict: ACCEPTED

input
99086 49261
283011093 775415197 945248913 ...

correct output
162897193427

user output
162897193427

Test 17

Verdict: ACCEPTED

input
95142 68716
163121171 100246843 163249744 ...

correct output
181966887510

user output
181966887510

Test 18

Verdict: ACCEPTED

input
19 3
49 73 32 44 84 29 9 76 77 36 9...

correct output
147

user output
147

Test 19

Verdict: ACCEPTED

input
19 6
68 94 70 46 91 47 67 100 62 23...

correct output
244

user output
244

Test 20

Verdict: ACCEPTED

input
18 3
90 65 90 80 41 5 95 17 53 7 29...

correct output
127

user output
127

Test 21

Verdict: ACCEPTED

input
16 1
48 91 87 2 16 39 92 26 1 68 78...

correct output
224

user output
224

Test 22

Verdict: ACCEPTED

input
15 2
11 1 37 1 8 20 24 59 42 52 91 ...

correct output
219

user output
219

Test 23

Verdict: ACCEPTED

input
94171 47986
552466372 215830212 408722990 ...

correct output
150760767335

user output
150760767335

Test 24

Verdict: ACCEPTED

input
94221 86537
45774448 992088337 309200563 7...

correct output
105016024608

user output
105016024608

Test 25

Verdict: ACCEPTED

input
96867 68461
39428919 749383323 481367036 5...

correct output
219609902823

user output
219609902823

Test 26

Verdict: ACCEPTED

input
96077 83268
95064821 23000135 276477667 48...

correct output
140845793522

user output
140845793522

Test 27

Verdict: ACCEPTED

input
99950 74867
864529930 269063846 767129921 ...

correct output
217758011647

user output
217758011647

Test 28

Verdict: ACCEPTED

input
97182 41080
671075894 704355936 662862756 ...

correct output
167687874177

user output
167687874177

Test 29

Verdict: ACCEPTED

input
96113 18731
554071104 111523361 276689875 ...

correct output
207695993751

user output
207695993751

Test 30

Verdict: ACCEPTED

input
92311 6858
440850686 890390689 572816739 ...

correct output
150986466317

user output
150986466317

Test 31

Verdict: ACCEPTED

input
99999 25726
949775640 24729596 158203669 2...

correct output
125772383324

user output
125772383324

Test 32

Verdict: ACCEPTED

input
97857 32129
20264277 670255203 316861628 1...

correct output
182392362837

user output
182392362837