#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
using ll = long long;
int magic;
int n;
vector<ll> dp1[maxn], dp2[maxn];
int A[maxn], B[maxn];
ll C[maxn], D[maxn];
ll pre[maxn];
vector<int> g[maxn];
ll tot_down = 0;
int sz[maxn];
int other(int id, int u) {
return A[id] ^ B[id] ^ u;
}
ll cost(int id, int from, int to) {
if(A[id] == from) return C[id];
else return D[id];
}
void dfs(int u, int parent, ll prefix_sum) {
pre[u] = prefix_sum;
for(int i : g[u]) {
int v = A[i] ^ B[i] ^ u;
if(parent != v) {
dfs(v, u, prefix_sum + cost(i, v, u) - cost(i, u, v));
}
}
sz[u] = 1;
ll tmp[magic + 15];
for(int i : g[u]) {
int v = A[i] ^ B[i] ^ u;
if(v == parent) continue;
ll w = cost(i, u, v);
tot_down += w;
for(int a = 1; a <= min(magic, sz[u]); a++) {
for(int b = 1; b <= min(magic, sz[v]); b++) {
dp1[u][a + b] = max(dp1[u][a + b], dp2[u][a] + dp2[v][b] + w);
}
}
for(int i = 0; i <= min(magic, sz[u] + sz[v]); i++)
tmp[i] = 0;
for(int a = 0; a <= min(sz[u], magic); a++) {
for(int b = 1; b <= min(sz[v], magic); b++) {
tmp[a + b] = max(tmp[a + b], dp2[u][a] + dp2[v][b] + w);
}
}
sz[u] += sz[v];
for(int i = 0; i <= min(sz[u], magic); i++) {
dp2[u][i] = max(dp2[u][i], tmp[i]);
}
}
}
int main(int argc, char const *argv[])
{
cin >> n;
if(n > 2000) magic = 5;
else magic = 2e3 + 2;
for(int i = 1; i <= n; i++) {
dp1[i].assign(magic + 15, 0);
dp2[i].assign(magic + 15, 0);
}
for(int i = 1; i < n; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
g[A[i]].emplace_back(i);
g[B[i]].emplace_back(i);
}
dfs(1, -1, 0);
int q; cin >> q;
while(q--) {
int k;
cin >> k;
ll ans = LLONG_MAX;
for(int i = 1; i <= n; i++) {
ans = min(ans, tot_down - dp1[i][min(k, sz[i])] + pre[i]);
}
ans = max(0LL, ans);
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14592 KB |
Output is correct |
2 |
Correct |
14 ms |
14976 KB |
Output is correct |
3 |
Correct |
14 ms |
14976 KB |
Output is correct |
4 |
Correct |
14 ms |
14976 KB |
Output is correct |
5 |
Correct |
14 ms |
14976 KB |
Output is correct |
6 |
Correct |
14 ms |
14976 KB |
Output is correct |
7 |
Correct |
15 ms |
14948 KB |
Output is correct |
8 |
Correct |
14 ms |
14976 KB |
Output is correct |
9 |
Correct |
13 ms |
14976 KB |
Output is correct |
10 |
Correct |
13 ms |
14948 KB |
Output is correct |
11 |
Correct |
13 ms |
14976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14592 KB |
Output is correct |
2 |
Correct |
665 ms |
97736 KB |
Output is correct |
3 |
Correct |
741 ms |
123256 KB |
Output is correct |
4 |
Correct |
647 ms |
97940 KB |
Output is correct |
5 |
Correct |
637 ms |
98052 KB |
Output is correct |
6 |
Correct |
653 ms |
101240 KB |
Output is correct |
7 |
Correct |
603 ms |
98256 KB |
Output is correct |
8 |
Correct |
730 ms |
123856 KB |
Output is correct |
9 |
Correct |
554 ms |
98664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
648 ms |
97948 KB |
Output is correct |
3 |
Correct |
771 ms |
132996 KB |
Output is correct |
4 |
Correct |
643 ms |
101960 KB |
Output is correct |
5 |
Correct |
652 ms |
103228 KB |
Output is correct |
6 |
Correct |
666 ms |
107284 KB |
Output is correct |
7 |
Correct |
586 ms |
103804 KB |
Output is correct |
8 |
Correct |
725 ms |
119160 KB |
Output is correct |
9 |
Correct |
569 ms |
103656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14592 KB |
Output is correct |
2 |
Correct |
14 ms |
14976 KB |
Output is correct |
3 |
Correct |
14 ms |
14976 KB |
Output is correct |
4 |
Correct |
14 ms |
14976 KB |
Output is correct |
5 |
Correct |
14 ms |
14976 KB |
Output is correct |
6 |
Correct |
14 ms |
14976 KB |
Output is correct |
7 |
Correct |
15 ms |
14948 KB |
Output is correct |
8 |
Correct |
14 ms |
14976 KB |
Output is correct |
9 |
Correct |
13 ms |
14976 KB |
Output is correct |
10 |
Correct |
13 ms |
14948 KB |
Output is correct |
11 |
Correct |
13 ms |
14976 KB |
Output is correct |
12 |
Correct |
14 ms |
14592 KB |
Output is correct |
13 |
Correct |
105 ms |
77816 KB |
Output is correct |
14 |
Correct |
121 ms |
78028 KB |
Output is correct |
15 |
Correct |
106 ms |
77816 KB |
Output is correct |
16 |
Correct |
102 ms |
77788 KB |
Output is correct |
17 |
Correct |
118 ms |
77728 KB |
Output is correct |
18 |
Correct |
108 ms |
77816 KB |
Output is correct |
19 |
Correct |
116 ms |
77844 KB |
Output is correct |
20 |
Correct |
117 ms |
77816 KB |
Output is correct |
21 |
Correct |
118 ms |
77788 KB |
Output is correct |
22 |
Correct |
106 ms |
77860 KB |
Output is correct |
23 |
Correct |
119 ms |
77952 KB |
Output is correct |
24 |
Correct |
118 ms |
77944 KB |
Output is correct |
25 |
Correct |
126 ms |
78072 KB |
Output is correct |
26 |
Correct |
111 ms |
77816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14592 KB |
Output is correct |
2 |
Correct |
665 ms |
97736 KB |
Output is correct |
3 |
Correct |
741 ms |
123256 KB |
Output is correct |
4 |
Correct |
647 ms |
97940 KB |
Output is correct |
5 |
Correct |
637 ms |
98052 KB |
Output is correct |
6 |
Correct |
653 ms |
101240 KB |
Output is correct |
7 |
Correct |
603 ms |
98256 KB |
Output is correct |
8 |
Correct |
730 ms |
123856 KB |
Output is correct |
9 |
Correct |
554 ms |
98664 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
648 ms |
97948 KB |
Output is correct |
12 |
Correct |
771 ms |
132996 KB |
Output is correct |
13 |
Correct |
643 ms |
101960 KB |
Output is correct |
14 |
Correct |
652 ms |
103228 KB |
Output is correct |
15 |
Correct |
666 ms |
107284 KB |
Output is correct |
16 |
Correct |
586 ms |
103804 KB |
Output is correct |
17 |
Correct |
725 ms |
119160 KB |
Output is correct |
18 |
Correct |
569 ms |
103656 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Incorrect |
665 ms |
103320 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14592 KB |
Output is correct |
2 |
Correct |
14 ms |
14976 KB |
Output is correct |
3 |
Correct |
14 ms |
14976 KB |
Output is correct |
4 |
Correct |
14 ms |
14976 KB |
Output is correct |
5 |
Correct |
14 ms |
14976 KB |
Output is correct |
6 |
Correct |
14 ms |
14976 KB |
Output is correct |
7 |
Correct |
15 ms |
14948 KB |
Output is correct |
8 |
Correct |
14 ms |
14976 KB |
Output is correct |
9 |
Correct |
13 ms |
14976 KB |
Output is correct |
10 |
Correct |
13 ms |
14948 KB |
Output is correct |
11 |
Correct |
13 ms |
14976 KB |
Output is correct |
12 |
Correct |
13 ms |
14592 KB |
Output is correct |
13 |
Correct |
665 ms |
97736 KB |
Output is correct |
14 |
Correct |
741 ms |
123256 KB |
Output is correct |
15 |
Correct |
647 ms |
97940 KB |
Output is correct |
16 |
Correct |
637 ms |
98052 KB |
Output is correct |
17 |
Correct |
653 ms |
101240 KB |
Output is correct |
18 |
Correct |
603 ms |
98256 KB |
Output is correct |
19 |
Correct |
730 ms |
123856 KB |
Output is correct |
20 |
Correct |
554 ms |
98664 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
648 ms |
97948 KB |
Output is correct |
23 |
Correct |
771 ms |
132996 KB |
Output is correct |
24 |
Correct |
643 ms |
101960 KB |
Output is correct |
25 |
Correct |
652 ms |
103228 KB |
Output is correct |
26 |
Correct |
666 ms |
107284 KB |
Output is correct |
27 |
Correct |
586 ms |
103804 KB |
Output is correct |
28 |
Correct |
725 ms |
119160 KB |
Output is correct |
29 |
Correct |
569 ms |
103656 KB |
Output is correct |
30 |
Correct |
14 ms |
14592 KB |
Output is correct |
31 |
Correct |
105 ms |
77816 KB |
Output is correct |
32 |
Correct |
121 ms |
78028 KB |
Output is correct |
33 |
Correct |
106 ms |
77816 KB |
Output is correct |
34 |
Correct |
102 ms |
77788 KB |
Output is correct |
35 |
Correct |
118 ms |
77728 KB |
Output is correct |
36 |
Correct |
108 ms |
77816 KB |
Output is correct |
37 |
Correct |
116 ms |
77844 KB |
Output is correct |
38 |
Correct |
117 ms |
77816 KB |
Output is correct |
39 |
Correct |
118 ms |
77788 KB |
Output is correct |
40 |
Correct |
106 ms |
77860 KB |
Output is correct |
41 |
Correct |
119 ms |
77952 KB |
Output is correct |
42 |
Correct |
118 ms |
77944 KB |
Output is correct |
43 |
Correct |
126 ms |
78072 KB |
Output is correct |
44 |
Correct |
111 ms |
77816 KB |
Output is correct |
45 |
Correct |
13 ms |
14464 KB |
Output is correct |
46 |
Incorrect |
665 ms |
103320 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |