QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#877372 | #7215. Optimal BST | C1942huangjiaxu | WA | 1ms | 3968kb | C++14 | 603b | 2025-01-31 21:40:32 | 2025-01-31 21:40:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=4005;
typedef long long ll;
int n,a[N];
ll s[N],f[N][N],ans[N];
vector<int>e[N];
void dfs(int x,int d){
f[d][d]=a[x],s[d]=s[d-1]+a[x];
for(int i=d-1,j=d-1;i;--i){
while(j>i&&f[i][j-1]+f[j][d]<=f[i][j]+f[j+1][d])--j;
f[i][d]=f[i][j]+f[j+1][d]+s[d]-s[i-1];
}
ans[x]=f[1][d];
for(auto v:e[x])dfs(v,d+1);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
for(int i=2,x;i<=n;++i){
scanf("%d",&x);
e[x].push_back(i);
}
dfs(1,1);
for(int i=1;i<=n;++i)printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3968kb
input:
3 1 2 3 1 2
output:
1 6 15
result:
ok 3 number(s): "1 6 15"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
3 1 2 3 1 1
output:
1 6 8
result:
ok 3 number(s): "1 6 8"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
2 1 1 1
output:
1 4
result:
ok 2 number(s): "1 4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
10 1 1 1 2 1 1 2 4 4 1 1 2 3 4 5 6 7 8 9
output:
1 4 8 15 20 25 34 50 67 75
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
10 1 2 3 4 3 3 4 3 4 3 1 1 1 1 3 4 3 8 2
output:
1 6 8 10 8 18 23 18 33 15
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 1ms
memory: 3968kb
input:
10 3 1 3 1 3 918724234 39934495 159260678 858678333 674749338 1 2 3 4 5 5 5 5 5
output:
3 8 18 24 37 1837448516 79869038 318521404 1717356714 1349498724
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
10 10 10 6 4 6 8 8 3 8 5 1 2 2 2 1 2 2 4 3
output:
10 40 68 62 68 36 74 59 96 93
result:
ok 10 numbers
Test #8:
score: -100
Wrong Answer
time: 0ms
memory: 3968kb
input:
10 5 1 1 3 1 693214231 733513667 158870252 465904460 113578073 1 2 3 4 5 5 5 5 5
output:
5 12 16 29 35 1386428508 1467027380 317740550 931808966 227156192
result:
wrong answer 4th numbers differ - expected: '27', found: '29'