#include<bits/stdc++.h>
using namespace std;
const long long maxn=2000+10;
struct yal{
long long u,v,wu,wv,jam;
long long getad(long long fu){
if(fu==u){
return v;
}
return u;
}
long long getval(long long fu){
if(fu==u){
return wu;
}
return wv;
}
}alle[maxn];
vector<long long>adj[maxn];
long long resr[maxn][maxn],kaf=0,res[maxn],suma,n,cnt=0,val[maxn];
set<pair<long long,long long>>dp[maxn];
void merge(long long u,long long v){
long long fu=u;
if((long long)dp[u].size()<(long long)dp[v].size()){
swap(u,v);
}
for(auto x:dp[v]){
dp[u].insert(x);
}
if(fu!=u){
swap(dp[fu],dp[u]);
}
}
long long dfs(long long u,long long p=-1,long long len=0){
// dp[u].clear();
// if(p!=-1){
// dp[u].insert(make_pair(0,u));
// }
val[u]=0;
long long mx=0,rt=u;
for(auto x:adj[u]){
long long v=alle[x].getad(u);
if(v!=p){
kaf+=alle[x].getval(u);
int z=dfs(v,u,alle[x].getval(v));
// merge(u,v);
if(val[z]>=mx){
rt=z;
mx=val[z];
}
}
}
// auto x=*dp[u].rbegin();
// dp[u].erase(x);
// x.first+=len;
// dp[u].insert(x);
val[rt]+=len;
return rt;
}
void solve(long long u){
kaf=0;
dfs(u);
resr[u][0]=kaf;
vector<long long>tof;
for(int i=1;i<=n;i++){
tof.push_back(val[i]);
}
sort(tof.begin(),tof.end());
for(long long i=1;i<=n;i++){
resr[u][i]=resr[u][i-1];
if((long long)tof.size()>0){
resr[u][i]+=(tof.back());
tof.pop_back();
}
res[i]=max(res[i],resr[u][i-1]);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(long long i=0;i<n-1;i++){
cin>>alle[i].u>>alle[i].v>>alle[i].wv>>alle[i].wu;
alle[i].jam=alle[i].wu+alle[i].wv;
suma+=alle[i].wu+alle[i].wv;
adj[alle[i].u].push_back(i);
adj[alle[i].v].push_back(i);
}
for(long long i=1;i<=n;i++){
solve(i);
}
for(long long i=1;i<=n;i++){
long long f=1;
for(long long j=2;j<=n;j++){
if(resr[i][j-1]!=res[j]){
f=0;
}
}
if(f){
cnt++;
}
}
if(cnt==1){
assert(0);
}
long long q;
cin>>q;
for(long long i=1;i<=n;i++){
res[i]=suma-res[i];
}
for(long long i=0;i<q;i++){
long long d;
cin>>d;
cout<<res[d]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
200 ms |
32092 KB |
Output is correct |
14 |
Correct |
137 ms |
32304 KB |
Output is correct |
15 |
Correct |
211 ms |
32368 KB |
Output is correct |
16 |
Correct |
207 ms |
32124 KB |
Output is correct |
17 |
Correct |
200 ms |
32084 KB |
Output is correct |
18 |
Correct |
195 ms |
32092 KB |
Output is correct |
19 |
Correct |
197 ms |
32084 KB |
Output is correct |
20 |
Correct |
205 ms |
32124 KB |
Output is correct |
21 |
Correct |
226 ms |
32292 KB |
Output is correct |
22 |
Correct |
185 ms |
32104 KB |
Output is correct |
23 |
Correct |
204 ms |
32100 KB |
Output is correct |
24 |
Correct |
232 ms |
32100 KB |
Output is correct |
25 |
Correct |
144 ms |
32280 KB |
Output is correct |
26 |
Correct |
231 ms |
32092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |