QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#589557 | #6430. Monster Hunter | ji_114514 | TL | 46ms | 10176kb | C++20 | 1.3kb | 2024-09-25 18:31:41 | 2024-09-25 18:31:42 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e3 + 10;
const ll INF = 1e18;
vector<int>e[N];
ll dp[N][N][2], a[N], t[N];
int n;
void dfs(int u)
{
for (int i = 0; i <= n; i++)dp[u][i][0] = dp[u][i][1] = INF;
dp[u][1][0] = 0, dp[u][0][1] = a[u];
for (auto v : e[u])
{
dfs(v);
for (int i = 0; i <= n; i++)t[i] = dp[u][i][0], dp[u][i][0] = INF;
for (int i = 0; i <= n; i++)
for (int j = 0; j + i <= n; j++)
dp[u][i + j][0] = min(dp[u][i + j][0], t[i] + min(dp[v][j][0], dp[v][j][1]));
for (int i = 0; i <= n; i++)t[i] = dp[u][i][1], dp[u][i][1] = INF;
for (int i = 0; i <= n; i++)
for (int j = 0; j + i <= n; j++)
dp[u][i + j][1] = min(dp[u][i + j][1], t[i] + min(dp[v][j][0], dp[v][j][1] + a[v]));
}
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)e[i].clear();
for (int i = 2; i <= n; i++)
{
int p; cin >> p;
e[p].push_back(i);
}
for (int i = 1; i <= n; i++)cin >> a[i];
dfs(1);
for (int i = 0; i <= n; i++)cout << min(dp[1][i][0], dp[1][i][1]) << ' ';
cout << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1; cin >> t;
while (t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3732kb
input:
3 5 1 2 3 4 1 2 3 4 5 9 1 2 3 4 3 4 6 6 8 4 9 4 4 5 2 4 1 12 1 2 2 4 5 3 4 3 8 10 11 9 1 3 5 10 10 7 3 7 9 4 9
output:
29 16 9 4 1 0 74 47 35 25 15 11 7 3 1 0 145 115 93 73 55 42 32 22 14 8 4 1 0
result:
ok 29 tokens
Test #2:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
179 20 1 1 1 4 5 5 7 7 9 9 11 12 13 14 5 16 17 16 19 3 9 3 2 7 7 2 8 5 7 5 4 7 4 2 4 9 2 7 9 19 1 1 3 4 3 6 7 6 6 10 10 12 13 13 12 16 16 18 8 8 3 6 10 1 1 1 2 2 3 3 3 10 5 5 7 10 5 2 1 10 4 2 1 2 7 14 1 1 3 4 4 6 4 8 9 10 11 8 13 4 4 6 6 10 8 9 5 7 1 4 7 9 8 6 1 2 3 3 5 2 7 5 6 1 6 11 1 2 3 3 5 6 6...
output:
209 182 159 137 117 99 81 65 56 47 39 32 25 19 15 11 8 6 4 2 0 178 151 129 108 89 74 64 54 44 36 29 23 18 13 10 7 5 2 1 0 18 4 0 16 2 0 172 137 111 93 78 63 49 39 31 23 16 10 5 1 0 52 33 21 9 3 1 0 109 72 53 39 29 22 16 10 5 2 1 0 105 69 47 35 25 17 12 7 3 0 156 133 113 97 82 68 56 44 33 26 ...
result:
ok 2178 tokens
Test #3:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
177 10 1 2 3 3 3 6 7 8 3 750920741 600457069 885939487 614833472 917972842 716271451 234536309 280049219 394290544 802674020 5 1 2 2 4 381361244 652246733 111336853 652733347 864548837 7 1 2 2 4 4 4 374076965 100213690 316923584 132783452 321143617 617096325 590521323 14 1 1 3 4 4 6 6 6 4 10 11 10 1...
output:
11644969567 6821338808 5469960998 4515572016 3564764256 2646791414 1844117394 1229283922 628826853 234536309 0 4943092784 2773077253 1145431444 492698097 111336853 0 4531440947 2737112778 2103265610 1466071235 823784001 365780594 100213690 0 15231373225 11922670663 9482266269 7732763282 605239618...
result:
ok 2176 tokens
Test #4:
score: 0
Accepted
time: 42ms
memory: 9940kb
input:
15 97 1 2 2 4 5 2 7 8 9 10 11 11 13 14 15 16 16 16 19 19 21 21 23 24 11 26 26 28 29 30 31 31 33 34 33 36 37 36 39 40 31 42 43 44 43 46 42 48 42 50 51 52 51 54 54 50 57 58 57 28 61 62 62 64 61 66 67 67 67 70 70 72 67 74 67 76 77 61 79 80 81 82 83 82 85 85 87 88 89 88 91 88 93 82 95 96 727261601 26107...
output:
107163950031 102732928896 98999901499 95286804744 91721257442 88258895738 85278119210 82530570741 79871508580 77382736169 74969945304 72561689905 70288550600 68157169720 66078603230 64055840678 62145527840 60274097298 58428791326 56711378906 54998171184 53305415604 51640777030 50049643668 4848686165...
result:
ok 2010 tokens
Test #5:
score: 0
Accepted
time: 46ms
memory: 10176kb
input:
13 171 1 1 3 3 5 6 6 6 9 10 11 12 13 14 15 16 12 6 19 20 20 22 22 24 25 22 27 28 29 27 27 27 33 33 33 36 37 38 39 40 40 42 39 44 33 46 33 48 48 48 51 51 53 53 55 56 53 58 59 48 61 61 63 64 65 66 67 68 69 70 69 72 73 73 72 69 77 69 68 80 81 82 83 84 85 82 87 88 80 90 91 92 93 93 95 93 97 98 99 91 101...
output:
187664021257 183101322620 178652629666 174903443687 171181863321 167508022266 164048979331 160694988110 157366569229 154144572418 150953357906 147898194220 145072963639 142252129359 139436947703 136631607337 133985709265 131363892815 128765304704 126168063880 123664992681 121166439175 118792530215 1...
result:
ok 1969 tokens
Test #6:
score: -100
Time Limit Exceeded
input:
2 1000 1 2 3 4 5 6 6 8 8 10 11 12 13 14 12 16 17 16 19 20 19 22 22 24 25 26 24 28 29 19 31 32 33 34 35 34 34 33 32 40 41 42 43 42 4 46 47 48 49 49 48 52 53 53 55 56 56 58 58 60 61 62 60 64 65 65 65 68 68 68 71 64 73 64 75 76 77 77 64 80 81 82 81 81 85 86 85 88 88 90 88 92 92 94 95 96 97 97 99 99 101...
output:
1113237056588 1108226059331 1103452926208 1098937464290 1094424834414 1090101107734 1085979381309 1081924835645 1077939941478 1073955392448 1070060014619 1066167943999 1062419631658 1058704177859 1055039647440 1051385794327 1047741820417 1044141128405 1040544084361 1036983950782 1033443541341 102993...