#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 2e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
ll lz[N << 2], n, lmao[N], par[N], uwu[N], up[N], dw[N], f[N], g[N], tmp[4], dp[N][3],
fo[N], leafs, tin[N], tout[N], cnt, vis[N], u, v, w, z, q, ans[N], rf[N];
pll tree[N << 2], ed[N], owo[N];
vector <int> a[N];
void down (int i){
if (!lz[i]) return;
forr (j, i << 1, i << 1 | 1){
tree[j].st += lz[i];
lz[j] += lz[i];
}
lz[i] = 0;
}
void add (int i, int l, int r, int u, int v, ll w, ll z){
if (l > v || r < u) return;
if (l >= u && r <= v){
tree[i].st += w;
tree[i].nd += z;
lz[i] += w;
return;
}
down(i);
int mid = l + r >> 1;
add(i << 1, l, mid, u, v, w, z);
add(i << 1 | 1, mid + 1, r, u, v, w, z);
tree[i] = max(tree[i << 1], tree[i << 1 | 1]);
}
void dfs (int u, int p){
uwu[u] = u;
owo[u] = {u, u};
for (int i : a[u]){
int v = u ^ ed[i].st ^ ed[i].nd;
ll w = up[i], z = dw[i];
if (u == ed[i].st) swap(w, z);
if (v == p) continue;
dfs(v, u);
f[u] += f[v] + w;
forr (j, 0, 2){
tmp[j] = dp[u][j] + dp[v][0] + w;
}
if (maxz(tmp[2], dp[u][0] + dp[v][2] + z)){
owo[u] = owo[v];
}
if (maxz(tmp[2], dp[u][1] + dp[v][1] + w + z)){
owo[u] = {uwu[u], uwu[v]};
}
if (maxz(tmp[1], dp[u][0] + dp[v][1] + w + z)){
uwu[u] = uwu[v];
}
forr (i, 0, 2){
dp[u][i] = tmp[i];
}
}
}
void dfs2 (int u, int p){
for (int i : a[u]){
int v = u ^ ed[i].st ^ ed[i].nd;
ll w = up[i], z = dw[i];
if (u == ed[i].st) swap(w, z);
if (v == p) continue;
fo[v] = fo[u] + f[u] - f[v] - w + z;
dfs2(v, u);
}
}
void dfs3 (int u, int p){
tin[u] = ++cnt;
if (a[u].size() == 1 && u != p){
leafs++;
add(1, 1, n, tin[u], tin[u], g[u], u);
}
for (int i : a[u]){
int v = u ^ ed[i].st ^ ed[i].nd;
ll w = up[i], z = dw[i];
if (u == ed[i].st) swap(w, z);
if (v == p) continue;
g[v] = g[u] + z;
par[v] = u;
lmao[v] = z;
dfs3(v, u);
}
tout[u] = cnt;
}
void bem (int u){
add(1, 1, n, tin[u], tout[u], -lmao[u], 0);
if (!vis[par[u]]){
vis[par[u]] = 1;
bem(par[u]);
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef kaguya
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n;
ll sum = 0;
forf (i, 1, n){
cin >> u >> v >> w >> z;
ed[i] = {u, v};
up[i] = w;
dw[i] = z;
a[u].pb(i);
a[v].pb(i);
sum += w + z;
}
dfs(1, 1);
dfs2(1, 1);
dfs3(owo[1].st, owo[1].st);
vis[owo[1].st] = 1;
leafs++;
forr (i, 2, leafs){
ans[i] = ans[i - 1] + tree[1].st;
if (i == 2){
ans[i] += f[owo[1].st] + fo[owo[1].st];
}
bem(tree[1].nd);
}
forr (i, 1, n){
maxz(ans[1], f[i] + fo[i]);
}
cin >> q;
while (q--){
cin >> u;
cout << sum - ans[min(u, leafs)] << "\n";
}
return 0;
}
/*
*/
Compilation message
designated_cities.cpp: In function 'void add(int, int, int, int, int, ll, ll)':
designated_cities.cpp:66:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23120 KB |
Output is correct |
2 |
Correct |
3 ms |
23120 KB |
Output is correct |
3 |
Correct |
4 ms |
19196 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
4 ms |
23120 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
25168 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
4 ms |
25168 KB |
Output is correct |
10 |
Correct |
4 ms |
19192 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25168 KB |
Output is correct |
2 |
Correct |
421 ms |
53720 KB |
Output is correct |
3 |
Correct |
328 ms |
69192 KB |
Output is correct |
4 |
Correct |
294 ms |
53828 KB |
Output is correct |
5 |
Correct |
297 ms |
53700 KB |
Output is correct |
6 |
Correct |
320 ms |
55624 KB |
Output is correct |
7 |
Correct |
296 ms |
54948 KB |
Output is correct |
8 |
Correct |
362 ms |
69192 KB |
Output is correct |
9 |
Correct |
250 ms |
48316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19192 KB |
Output is correct |
2 |
Correct |
272 ms |
53832 KB |
Output is correct |
3 |
Correct |
280 ms |
71512 KB |
Output is correct |
4 |
Correct |
268 ms |
53844 KB |
Output is correct |
5 |
Correct |
277 ms |
53700 KB |
Output is correct |
6 |
Correct |
333 ms |
56136 KB |
Output is correct |
7 |
Correct |
223 ms |
54204 KB |
Output is correct |
8 |
Correct |
383 ms |
63344 KB |
Output is correct |
9 |
Correct |
244 ms |
48316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23120 KB |
Output is correct |
2 |
Correct |
3 ms |
23120 KB |
Output is correct |
3 |
Correct |
4 ms |
19196 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
4 ms |
23120 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
25168 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
4 ms |
25168 KB |
Output is correct |
10 |
Correct |
4 ms |
19192 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
4 ms |
19024 KB |
Output is correct |
13 |
Correct |
6 ms |
15440 KB |
Output is correct |
14 |
Correct |
4 ms |
15440 KB |
Output is correct |
15 |
Correct |
5 ms |
15424 KB |
Output is correct |
16 |
Correct |
5 ms |
17488 KB |
Output is correct |
17 |
Correct |
4 ms |
13392 KB |
Output is correct |
18 |
Correct |
6 ms |
15440 KB |
Output is correct |
19 |
Correct |
5 ms |
15440 KB |
Output is correct |
20 |
Correct |
5 ms |
15440 KB |
Output is correct |
21 |
Correct |
5 ms |
15260 KB |
Output is correct |
22 |
Correct |
5 ms |
17488 KB |
Output is correct |
23 |
Correct |
5 ms |
15440 KB |
Output is correct |
24 |
Correct |
6 ms |
19536 KB |
Output is correct |
25 |
Correct |
5 ms |
17488 KB |
Output is correct |
26 |
Correct |
6 ms |
23376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25168 KB |
Output is correct |
2 |
Correct |
421 ms |
53720 KB |
Output is correct |
3 |
Correct |
328 ms |
69192 KB |
Output is correct |
4 |
Correct |
294 ms |
53828 KB |
Output is correct |
5 |
Correct |
297 ms |
53700 KB |
Output is correct |
6 |
Correct |
320 ms |
55624 KB |
Output is correct |
7 |
Correct |
296 ms |
54948 KB |
Output is correct |
8 |
Correct |
362 ms |
69192 KB |
Output is correct |
9 |
Correct |
250 ms |
48316 KB |
Output is correct |
10 |
Correct |
4 ms |
19192 KB |
Output is correct |
11 |
Correct |
272 ms |
53832 KB |
Output is correct |
12 |
Correct |
280 ms |
71512 KB |
Output is correct |
13 |
Correct |
268 ms |
53844 KB |
Output is correct |
14 |
Correct |
277 ms |
53700 KB |
Output is correct |
15 |
Correct |
333 ms |
56136 KB |
Output is correct |
16 |
Correct |
223 ms |
54204 KB |
Output is correct |
17 |
Correct |
383 ms |
63344 KB |
Output is correct |
18 |
Correct |
244 ms |
48316 KB |
Output is correct |
19 |
Correct |
3 ms |
17152 KB |
Output is correct |
20 |
Correct |
332 ms |
53840 KB |
Output is correct |
21 |
Correct |
320 ms |
77984 KB |
Output is correct |
22 |
Correct |
303 ms |
58620 KB |
Output is correct |
23 |
Correct |
368 ms |
60228 KB |
Output is correct |
24 |
Correct |
352 ms |
58996 KB |
Output is correct |
25 |
Correct |
279 ms |
60232 KB |
Output is correct |
26 |
Correct |
328 ms |
58952 KB |
Output is correct |
27 |
Correct |
299 ms |
60232 KB |
Output is correct |
28 |
Correct |
331 ms |
62280 KB |
Output is correct |
29 |
Correct |
308 ms |
60312 KB |
Output is correct |
30 |
Correct |
303 ms |
59332 KB |
Output is correct |
31 |
Correct |
352 ms |
60512 KB |
Output is correct |
32 |
Correct |
434 ms |
70304 KB |
Output is correct |
33 |
Correct |
300 ms |
54972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23120 KB |
Output is correct |
2 |
Correct |
3 ms |
23120 KB |
Output is correct |
3 |
Correct |
4 ms |
19196 KB |
Output is correct |
4 |
Correct |
4 ms |
19024 KB |
Output is correct |
5 |
Correct |
4 ms |
23120 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
25168 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
4 ms |
25168 KB |
Output is correct |
10 |
Correct |
4 ms |
19192 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
4 ms |
25168 KB |
Output is correct |
13 |
Correct |
421 ms |
53720 KB |
Output is correct |
14 |
Correct |
328 ms |
69192 KB |
Output is correct |
15 |
Correct |
294 ms |
53828 KB |
Output is correct |
16 |
Correct |
297 ms |
53700 KB |
Output is correct |
17 |
Correct |
320 ms |
55624 KB |
Output is correct |
18 |
Correct |
296 ms |
54948 KB |
Output is correct |
19 |
Correct |
362 ms |
69192 KB |
Output is correct |
20 |
Correct |
250 ms |
48316 KB |
Output is correct |
21 |
Correct |
4 ms |
19192 KB |
Output is correct |
22 |
Correct |
272 ms |
53832 KB |
Output is correct |
23 |
Correct |
280 ms |
71512 KB |
Output is correct |
24 |
Correct |
268 ms |
53844 KB |
Output is correct |
25 |
Correct |
277 ms |
53700 KB |
Output is correct |
26 |
Correct |
333 ms |
56136 KB |
Output is correct |
27 |
Correct |
223 ms |
54204 KB |
Output is correct |
28 |
Correct |
383 ms |
63344 KB |
Output is correct |
29 |
Correct |
244 ms |
48316 KB |
Output is correct |
30 |
Correct |
4 ms |
19024 KB |
Output is correct |
31 |
Correct |
6 ms |
15440 KB |
Output is correct |
32 |
Correct |
4 ms |
15440 KB |
Output is correct |
33 |
Correct |
5 ms |
15424 KB |
Output is correct |
34 |
Correct |
5 ms |
17488 KB |
Output is correct |
35 |
Correct |
4 ms |
13392 KB |
Output is correct |
36 |
Correct |
6 ms |
15440 KB |
Output is correct |
37 |
Correct |
5 ms |
15440 KB |
Output is correct |
38 |
Correct |
5 ms |
15440 KB |
Output is correct |
39 |
Correct |
5 ms |
15260 KB |
Output is correct |
40 |
Correct |
5 ms |
17488 KB |
Output is correct |
41 |
Correct |
5 ms |
15440 KB |
Output is correct |
42 |
Correct |
6 ms |
19536 KB |
Output is correct |
43 |
Correct |
5 ms |
17488 KB |
Output is correct |
44 |
Correct |
6 ms |
23376 KB |
Output is correct |
45 |
Correct |
3 ms |
17152 KB |
Output is correct |
46 |
Correct |
332 ms |
53840 KB |
Output is correct |
47 |
Correct |
320 ms |
77984 KB |
Output is correct |
48 |
Correct |
303 ms |
58620 KB |
Output is correct |
49 |
Correct |
368 ms |
60228 KB |
Output is correct |
50 |
Correct |
352 ms |
58996 KB |
Output is correct |
51 |
Correct |
279 ms |
60232 KB |
Output is correct |
52 |
Correct |
328 ms |
58952 KB |
Output is correct |
53 |
Correct |
299 ms |
60232 KB |
Output is correct |
54 |
Correct |
331 ms |
62280 KB |
Output is correct |
55 |
Correct |
308 ms |
60312 KB |
Output is correct |
56 |
Correct |
303 ms |
59332 KB |
Output is correct |
57 |
Correct |
352 ms |
60512 KB |
Output is correct |
58 |
Correct |
434 ms |
70304 KB |
Output is correct |
59 |
Correct |
300 ms |
54972 KB |
Output is correct |
60 |
Correct |
3 ms |
19024 KB |
Output is correct |
61 |
Correct |
364 ms |
62896 KB |
Output is correct |
62 |
Correct |
300 ms |
77364 KB |
Output is correct |
63 |
Correct |
271 ms |
61344 KB |
Output is correct |
64 |
Correct |
302 ms |
62792 KB |
Output is correct |
65 |
Correct |
287 ms |
61444 KB |
Output is correct |
66 |
Correct |
342 ms |
62792 KB |
Output is correct |
67 |
Correct |
314 ms |
61448 KB |
Output is correct |
68 |
Correct |
326 ms |
63048 KB |
Output is correct |
69 |
Correct |
392 ms |
64408 KB |
Output is correct |
70 |
Correct |
359 ms |
62752 KB |
Output is correct |
71 |
Correct |
348 ms |
62124 KB |
Output is correct |
72 |
Correct |
409 ms |
63936 KB |
Output is correct |
73 |
Correct |
479 ms |
72252 KB |
Output is correct |
74 |
Correct |
325 ms |
59132 KB |
Output is correct |