// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969;
ll sum, ans, total;
ll dp[N], ans_t[N];
bool vis[N];
vector<pair<ll, ll> > adj[N];
/*
1 -> pain
0 -> bala
*/
void preDFS(int x, int p = 0) {
dp[x] = vis[x];
for(auto j : adj[x]) {
if(j.first == p) {
continue;
}
preDFS(j.first, x);
dp[x] += dp[j.first];
}
}
void mainDFS(int x, int p = 0, int val = 0) {
if(dp[x] > 0) {
ans += val;
}
for(auto j : adj[x]) {
if(j.first == p) {
if(dp[x] < total) {
ans += j.second;
}
continue;
}
mainDFS(j.first, x, j.second);
}
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
int n, q; cin >>n;
assert(n <= 16);
for(int i = 0; i < n - 1; i++) {
int u, v, c, d; cin >>u >>v >>c >>d;
adj[u].push_back({v, c}), adj[v].push_back({u, d});
sum += c, sum += d;
}
for(int mask = 0; mask < (1 << n); mask++) {
for(int i = 0; i < n; i++) {
if((mask >> i) & 1) {
vis[i + 1] = 1;
}
}
ans = 0;
total = __builtin_popcount(mask);
preDFS(1);
mainDFS(1);
ans_t[total] = max(ans_t[total], ans);
for(int i = 0; i < n; i++) {
if((mask >> i) & 1) {
vis[i + 1] = 0;
}
}
}
cin >>q;
for(int i = 0; i < q; i++) {
int t; cin >>t;
cout<<sum - ans_t[t] <<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
32 ms |
23816 KB |
Output is correct |
3 |
Correct |
33 ms |
23816 KB |
Output is correct |
4 |
Correct |
33 ms |
23764 KB |
Output is correct |
5 |
Correct |
34 ms |
23892 KB |
Output is correct |
6 |
Correct |
35 ms |
23764 KB |
Output is correct |
7 |
Correct |
34 ms |
23764 KB |
Output is correct |
8 |
Correct |
35 ms |
23812 KB |
Output is correct |
9 |
Correct |
35 ms |
23812 KB |
Output is correct |
10 |
Correct |
33 ms |
23820 KB |
Output is correct |
11 |
Correct |
32 ms |
23768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23796 KB |
Output is correct |
2 |
Runtime error |
32 ms |
48096 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Runtime error |
31 ms |
48008 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
32 ms |
23816 KB |
Output is correct |
3 |
Correct |
33 ms |
23816 KB |
Output is correct |
4 |
Correct |
33 ms |
23764 KB |
Output is correct |
5 |
Correct |
34 ms |
23892 KB |
Output is correct |
6 |
Correct |
35 ms |
23764 KB |
Output is correct |
7 |
Correct |
34 ms |
23764 KB |
Output is correct |
8 |
Correct |
35 ms |
23812 KB |
Output is correct |
9 |
Correct |
35 ms |
23812 KB |
Output is correct |
10 |
Correct |
33 ms |
23820 KB |
Output is correct |
11 |
Correct |
32 ms |
23768 KB |
Output is correct |
12 |
Correct |
12 ms |
23812 KB |
Output is correct |
13 |
Runtime error |
30 ms |
48084 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23796 KB |
Output is correct |
2 |
Runtime error |
32 ms |
48096 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
32 ms |
23816 KB |
Output is correct |
3 |
Correct |
33 ms |
23816 KB |
Output is correct |
4 |
Correct |
33 ms |
23764 KB |
Output is correct |
5 |
Correct |
34 ms |
23892 KB |
Output is correct |
6 |
Correct |
35 ms |
23764 KB |
Output is correct |
7 |
Correct |
34 ms |
23764 KB |
Output is correct |
8 |
Correct |
35 ms |
23812 KB |
Output is correct |
9 |
Correct |
35 ms |
23812 KB |
Output is correct |
10 |
Correct |
33 ms |
23820 KB |
Output is correct |
11 |
Correct |
32 ms |
23768 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Runtime error |
32 ms |
48096 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |