#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 301010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
vector<int> adj[MAX];
int C[MAX];
int rem[MAX];
ll ans = 0;
void dfs(int x, int p = 0) {
if (adj[x].size() == 1 && p) {
rem[x] = C[x];
ans += C[x];
return;
}
for (auto v : adj[x]) if (v != p) {
dfs(v, x);
if (rem[v] >= C[x]) rem[x] += rem[v] - C[x];
}
for (auto v : adj[x]) if (v != p) {
if (rem[v] < C[x]) {
rem[x] -= C[x] - rem[v];
ans += C[x] - rem[v];
}
}
if (rem[x] < 0) {
ans -= rem[x];
rem[x] = 0;
}
if (!p) return;
if (rem[x] < C[x]) {
ans += C[x] - rem[x];
rem[x] = C[x];
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N, M, Q;
cin >> N >> M >> Q;
int i;
for (i = 1; i <= N; i++) cin >> C[i];
int a, b;
for (i = 1; i < N; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
cout << ans << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
73 ms |
30776 KB |
Output is correct |
3 |
Correct |
62 ms |
30780 KB |
Output is correct |
4 |
Incorrect |
63 ms |
30748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
73 ms |
30776 KB |
Output is correct |
3 |
Correct |
62 ms |
30780 KB |
Output is correct |
4 |
Incorrect |
63 ms |
30748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
73 ms |
30776 KB |
Output is correct |
3 |
Correct |
62 ms |
30780 KB |
Output is correct |
4 |
Incorrect |
63 ms |
30748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
73 ms |
30776 KB |
Output is correct |
3 |
Correct |
62 ms |
30780 KB |
Output is correct |
4 |
Incorrect |
63 ms |
30748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
73 ms |
30776 KB |
Output is correct |
3 |
Correct |
62 ms |
30780 KB |
Output is correct |
4 |
Incorrect |
63 ms |
30748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |