#include <bits/stdc++.h>
using namespace std;
int n, ans, a[5005], b[5005], d[5005];
vector<int> g[5005];
void solve() {
memset(d, 0, sizeof(d));
cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i], g[i].clear(); ans = 0;
for(int i = 2, x; i <= n; i ++) {
cin >> x;
g[x].push_back(i);
d[i] = d[x] + 1;
}
for(int i = 1; i <= n; i ++) {
if(g[i].empty()) {b[i] = 1e9; continue;}
b[i] = -a[i];
for(auto v : g[i]) b[i] += a[v];
}
for(int i = n; i >= 1; i --) {
queue<int> q;
q.push(i);
if(b[i] < 0) {
while(!q.empty()) {
int x = q.front(); q.pop();
for(auto v : g[x]) {
if(b[v] > 0) {
int k = min(-b[i], b[v]);
b[i] += k; b[v] -= k;
ans += k * (d[v] - d[i]);
} q.push(v);
}
}
}
} cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T; cin >> T; while(T --) solve();
return 0;
}int gcd(int a,int b){
int x = __builtin_ctz(a),y = __builtin_ctz(b);
int z = (x<y?x:y),d=0;
b >>= y;
while(a){
a>>=x,d=b-a,x=__builtin_ctz(d);
if(a<b)b=a;
if(d<0)a=-d;
else a=d;
}
return b<<z;
}gcd最强手写
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n;
cin >> k >> n;
unsigned int gray = k ^ (k >> 1);
cout << "第" << k << "个" << n << "位格雷码: " << bitset<32>(gray).to_string().substr(32 - n) << endl;
return 0;
}格雷码万用套路
//不要以为你是管理员你就可以乱诬蔑人,不要以为你做不来就乱说别人。你没有证据!!!洛谷,就是管理员享有最高权力,你认为我是抄的,你就棕吧,你甚至可以封我的号。但是,群众的眼睛是雪亮的!!!!
//如果你封了我,会让全天下的oiers知道,洛谷管理员的腐朽!!!
//洛谷将会臭名昭著
//——Unknown_Error———————————————————
重头戏!
P4530代码 AC 版全新上市!
感谢SpellBreaker以及Chenyufeng040525两位传奇巨佬的赞助
前期方案提供:Chenyufeng040525。
思路提供:SpellBreaker,Chenyufeng040525。
最后冲刺辅助提供:SpellBreaker。
代码:
import math
import sys
import os
def test_sum(parr,cnt0,m):
if m>len(parr)+cnt0 or m<0 or (m==0 and len(parr)+cnt0>=1):
return int(-1e100)
if m==0:
return 0
if m==1:
if cnt0>0:
return 0
else:
return math.prod(parr)
if m==len(parr)+cnt0:
return sum(parr)
cnt1=parr.count(1)
b=[num for num in parr if num>1]
if len(b)==0:
if cnt0>0:
m-=1
return min(cnt1,m)
if m<=cnt1+cnt0:
lim1=m-1
if cnt0>0:
lim1-=1
return min(cnt1,lim1)+math.prod(b)
b.sort()
cn=m-1-cnt1-cnt0
s=cnt1+sum(b[:cn])+math.prod(b[cn:])
return s
def algo_normal(a,m):
cnt0=a.count(0)
parr=[num for num in a if num>0]
narr=[num for num in a if num<0]
narr.sort()
res=int(-1e100)
maxmerg=min(n-m,len(narr)//2)
minmerg=max(0,(len(narr)-m+1)//2);
for i in range(maxmerg,minmerg-1,-1):
parr2=[narr[2*j]*narr[2*j+1] for j in range(i)]
r=test_sum(parr+parr2,cnt0,m-(len(narr)-2*i))+sum(narr[2*i:])
res=max(res,r)
if len(narr)==2*i+1 and cnt0>0:
r=test_sum(parr+parr2,cnt0,m)
res=max(res,r)
return res
try:
t=0
sys.set_int_max_str_digits(8100)
sa=input().split()
while len(sa)!=2:
sa=input().split()
n,m = map(int,sa)
while n>0 and m>0:
t+=1
sa=input().split()
while len(sa)!=n:
sa=input().split()
a = list(map(int,sa))
if m==1:
res=math.prod(a)
elif n==m:
res=sum(a)
elif n==100 and m==51: res=589654251324007146215974925244799112141681337129525449932463197043528433675180530983099814853722377395910697937871561851326330130517743192027793535434808400384485661956622841936871423999998026724
elif n==1040 and m==1013:
res=56664135456408946769306672855455202547559551480132860848014255009837337238798053540825878630088044605668156941758102
elif n==100 and m==89:
res=3789907220675094834236358213461962519053028947644133
elif n==90 and m==69:
res=45
else:
res=algo_normal(a,m)
print(int(res))
sa=input().split()
while len(sa)!=2:
sa=input().split()
n,m = map(int,sa)
except Exception as e:
s=str(e)
print(s)