#include<bits/stdc++.h>
//#include "deliveries.h"
using namespace std;
int n;
long long sum;
vector<int> w;
vector<vector<pair<int, int> > > adj;
void init(int N, std::vector<int> U, std::vector<int> V, std::vector<int> T, std::vector<int> W) {
n = N;
w = W;
adj.resize(n);
for(int i = 0; i + 1 < n; i++) {
adj[U[i]].emplace_back(V[i], T[i]);
adj[V[i]].emplace_back(U[i], T[i]);
}
sum = accumulate(W.begin(), W.end(), 0LL);
}
long long max_time(int s, int x) {
sum -= w[s];
w[s] = x;
sum += w[s];
vector<long long> sz(n);
long long ans = 0;
auto dfs = [&] (auto self, int u, int p) -> void {
sz[u] = w[u];
for(auto &[v, c] : adj[u]) if (v != p) {
self(self, v, u);
sz[u] += sz[v];
ans += 2LL * c * min(sum - sz[v] + 1, sz[v]);
}
};
dfs(dfs, 0, 0);
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> U(N - 1), V(N - 1);
for(int j = 0; j + 1 < N; j++)
assert (1 == scanf("%d", &U[j]));
for(int j = 0; j + 1 < N; j++)
assert (1 == scanf("%d", &V[j]));
std::vector<int> T(N - 1);
for(int j = 0; j + 1 < N; j++)
assert (1 == scanf("%d", &T[j]));
std::vector<int> W(N);
for(int i = 0; i < N; i++)
assert (1 == scanf("%d", &W[i]));
std::vector<int> S(Q);
std::vector<int> X(Q);
for(int k = 0; k < Q; k++)
assert (2 == scanf("%d %d", &S[k], &X[k]));
fclose(stdin);
init(N, U, V, T, W);
std::vector<long long> res(Q);
for(int k = 0; k < Q; k++)
res[k] = max_time(S[k], X[k]);
for(int k = 0; k < Q; k++)
printf("%lld\n", res[k]);
fclose(stdout);
return 0;
}
#endif // ngu
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
10320 KB |
Output is correct |
2 |
Correct |
72 ms |
10064 KB |
Output is correct |
3 |
Correct |
71 ms |
10288 KB |
Output is correct |
4 |
Correct |
71 ms |
10324 KB |
Output is correct |
5 |
Correct |
82 ms |
10344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
572 KB |
Output is correct |
5 |
Correct |
9 ms |
600 KB |
Output is correct |
6 |
Correct |
12 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
348 KB |
Output is correct |
8 |
Correct |
9 ms |
580 KB |
Output is correct |
9 |
Correct |
9 ms |
348 KB |
Output is correct |
10 |
Correct |
9 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
448 KB |
Output is correct |
12 |
Correct |
8 ms |
348 KB |
Output is correct |
13 |
Correct |
11 ms |
348 KB |
Output is correct |
14 |
Correct |
11 ms |
572 KB |
Output is correct |
15 |
Correct |
11 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
10320 KB |
Output is correct |
2 |
Correct |
72 ms |
10064 KB |
Output is correct |
3 |
Correct |
71 ms |
10288 KB |
Output is correct |
4 |
Correct |
71 ms |
10324 KB |
Output is correct |
5 |
Correct |
82 ms |
10344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
18 ms |
604 KB |
Output is correct |
8 |
Correct |
2096 ms |
2548 KB |
Output is correct |
9 |
Execution timed out |
5551 ms |
18736 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
10320 KB |
Output is correct |
2 |
Correct |
72 ms |
10064 KB |
Output is correct |
3 |
Correct |
71 ms |
10288 KB |
Output is correct |
4 |
Correct |
71 ms |
10324 KB |
Output is correct |
5 |
Correct |
82 ms |
10344 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
25 ms |
628 KB |
Output is correct |
8 |
Correct |
2611 ms |
2852 KB |
Output is correct |
9 |
Execution timed out |
5525 ms |
21808 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
10320 KB |
Output is correct |
2 |
Correct |
72 ms |
10064 KB |
Output is correct |
3 |
Correct |
71 ms |
10288 KB |
Output is correct |
4 |
Correct |
71 ms |
10324 KB |
Output is correct |
5 |
Correct |
82 ms |
10344 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
348 KB |
Output is correct |
9 |
Correct |
9 ms |
572 KB |
Output is correct |
10 |
Correct |
9 ms |
600 KB |
Output is correct |
11 |
Correct |
12 ms |
348 KB |
Output is correct |
12 |
Correct |
9 ms |
348 KB |
Output is correct |
13 |
Correct |
9 ms |
580 KB |
Output is correct |
14 |
Correct |
9 ms |
348 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
448 KB |
Output is correct |
17 |
Correct |
8 ms |
348 KB |
Output is correct |
18 |
Correct |
11 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
572 KB |
Output is correct |
20 |
Correct |
11 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
18 ms |
604 KB |
Output is correct |
23 |
Correct |
2096 ms |
2548 KB |
Output is correct |
24 |
Execution timed out |
5551 ms |
18736 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |