UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183517#3294. 分配X_X100121ms3596kbC++111.1kb2023-08-09 09:54:092023-08-09 12:32:53

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e3+3,M=5e3;
int n,x,a[N],h[N],idx,f[N],s[2][M+3];
struct node{
	int v,ne;
}e[N];
void add(int x,int y){
	e[++idx]={y,h[x]};
	h[x]=idx;
}
void dfs(int u){
	int son=0,sum=0;
	for(int i=h[u];i;i=e[i].ne){
		int v=e[i].v;
		dfs(v);
		sum+=a[v]+f[v];
	}
	for(int i=0;i<=a[u];i++) s[0][i]=s[1][i]=0;
	s[0][0]=1;
	for(int i=h[u];i;i=e[i].ne){
		int v=e[i].v;son++;
		for(int j=a[u];j>=0;j--) 
		    s[son&1][j]=0;
		for(int j=a[u];j>=f[v];j--)
		    s[son&1][j]|=s[(son-1)&1][j-f[v]];
		for(int j=a[u];j>=a[v];j--)
		    s[son&1][j]|=s[(son-1)&1][j-a[v]];
	}
	//cout<<u<<' '<<sum<<endl;
	//for(int i=0;i<=a[u];i++) cout<<s[son&1][i]<<' ';cout<<endl;
	for(int i=a[u];i>=0;i--)
    	if(s[son&1][i]){
    		f[u]=sum-i;
    		//cout<<f[u]<<endl;
    		return;
		}
	puts("IMPOSSIBLE");
	exit(0);	
}
int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++){
    	scanf("%d",&x);
    	add(x,i);
	}
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	dfs(1);
	puts("POSSIBLE");
 	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 2ms
memory: 3528kb

input:

20
1 2 1 3 2 4 6 7 6 4 10 11 8 4 14 3 5 10 11
8 21 13 15 10 1 0 8 12 17 15 0 12 0 0 2 13 1 3 4

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

20
1 1 3 4 4 6 3 3 4 5 2 12 4 11 9 4 4 5 9
37 0 21 29 11 0 4 0 0 6 12 8 0 0 0 0 0 7 10 6

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3464kb

input:

20
1 1 3 2 1 1 1 7 3 4 4 2 9 13 14 5 7 10 12
42 13 17 13 8 0 15 15 2 13 6 5 10 0 0 0 0 8 2 0

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

20
1 2 1 3 5 1 4 3 2 4 6 7 5 14 5 7 15 15 11
63 0 26 25 26 0 14 0 0 0 9 0 14 0 0 13 7 0 11 6

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3540kb

input:

20
1 2 1 1 1 1 3 2 5 8 5 7 4 5 12 9 11 18 2
9 2 0 8 13 0 7 7 2 0 0 0 0 8 0 0 0 0 0 9

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

20
1 1 2 3 4 3 7 6 4 1 6 8 13 6 8 11 14 5 1
17 24 6 4 4 2 6 6 9 0 0 0 3 6 2 15 0 3 4 7

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3456kb

input:

20
1 1 3 4 1 3 3 2 3 2 9 9 5 14 14 12 13 10 11
44 13 31 1 15 0 15 0 22 1 0 0 4 17 1 0 0 9 0 1

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #8:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

20
1 2 3 2 5 3 7 6 1 2 10 10 1 1 9 14 4 13 10
52 4 15 12 16 13 0 1 13 15 9 13 9 12 2 0 7 2 15 0

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3504kb

input:

20
1 2 3 3 4 3 5 2 1 9 1 11 11 3 7 15 3 1 1
16 10 22 3 10 0 9 1 0 0 3 0 0 0 0 0 0 0 0 0

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3472kb

input:

20
1 1 3 1 4 3 2 6 9 10 4 5 3 10 13 1 10 6 19
47 21 47 20 0 0 0 15 17 17 9 0 0 15 8 0 0 0 0 0

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Subtask #2:

score: 30
Accepted

Test #11:

score: 30
Accepted
time: 2ms
memory: 3264kb

input:

100
1 1 2 2 3 2 7 7 1 6 5 10 3 10 11 12 4 3 14 11 19 15 22 10 14 7 5 13 8 1 7 10 10 30 15 17 24 18 3...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

100
1 1 2 2 3 6 5 2 2 9 3 11 5 2 12 9 14 13 9 4 10 17 22 15 16 23 5 27 10 20 16 29 1 1 18 16 28 30 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #13:

score: 0
Accepted
time: 2ms
memory: 3528kb

input:

100
1 2 3 2 2 2 3 1 3 1 5 7 6 2 13 16 8 11 4 14 7 1 11 10 23 6 6 3 29 17 10 29 21 21 2 19 36 10 18 1...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3460kb

input:

100
1 1 3 3 3 2 5 3 8 4 10 2 5 14 10 1 1 1 4 13 2 20 1 2 19 2 18 5 5 21 5 1 23 21 7 7 34 32 26 22 35...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3380kb

input:

100
1 2 1 4 2 2 2 8 6 3 2 11 9 1 5 2 17 4 14 3 15 4 15 18 6 18 13 9 10 2 24 8 28 27 19 10 4 8 16 30 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #16:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

100
1 1 2 2 1 2 3 4 8 10 1 3 13 14 15 14 10 14 1 3 10 1 12 4 14 24 26 25 12 22 29 27 17 11 32 26 35 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #17:

score: 0
Accepted
time: 2ms
memory: 3520kb

input:

100
1 1 3 3 4 2 7 8 8 8 4 5 9 8 6 16 9 9 19 1 16 17 4 12 1 14 14 1 19 7 19 3 22 26 35 30 19 36 11 25...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #18:

score: 0
Accepted
time: 0ms
memory: 3468kb

input:

100
1 2 3 1 4 2 5 4 7 9 6 10 5 3 8 1 16 5 10 7 21 1 19 4 7 12 9 5 24 27 7 10 26 6 4 5 27 12 2 33 11 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #19:

score: 0
Accepted
time: 2ms
memory: 3512kb

input:

100
1 1 3 2 3 4 2 1 6 6 8 11 9 4 15 3 6 10 17 17 13 19 17 12 19 2 14 9 7 12 27 18 31 20 16 16 22 6 9...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #20:

score: 0
Accepted
time: 1ms
memory: 3464kb

input:

100
1 1 3 3 5 4 5 5 5 8 11 8 6 12 6 4 6 15 8 15 19 12 9 4 6 26 10 21 14 23 15 25 3 8 14 27 30 20 38 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Subtask #3:

score: 50
Accepted

Test #21:

score: 50
Accepted
time: 0ms
memory: 3528kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #22:

score: 0
Accepted
time: 3ms
memory: 3492kb

input:

1000
1 1 1 4 3 6 4 4 4 4 4 2 3 10 10 2 6 9 6 13 14 19 13 17 4 13 14 17 6 22 25 9 29 19 18 15 32 7 4 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #23:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

1000
1 1 2 3 4 5 5 7 8 9 10 11 12 12 14 14 15 16 18 19 20 19 20 23 24 24 25 27 28 29 30 31 32 32 32 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #24:

score: 0
Accepted
time: 2ms
memory: 3508kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #25:

score: 0
Accepted
time: 3ms
memory: 3480kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #26:

score: 0
Accepted
time: 1ms
memory: 3540kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #27:

score: 0
Accepted
time: 3ms
memory: 3548kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #28:

score: 0
Accepted
time: 3ms
memory: 3404kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #29:

score: 0
Accepted
time: 5ms
memory: 3480kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #30:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

1000
1 1 2 2 4 5 6 7 7 8 9 11 12 13 13 15 16 17 17 19 20 20 21 23 23 24 26 26 28 29 29 30 31 33 33 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #31:

score: 0
Accepted
time: 3ms
memory: 3528kb

input:

1000
1 1 1 3 3 2 6 8 4 2 6 3 1 13 8 14 17 2 17 18 16 16 3 13 23 21 24 17 5 23 20 22 1 33 5 36 12 30 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #32:

score: 0
Accepted
time: 3ms
memory: 3448kb

input:

500
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #33:

score: 0
Accepted
time: 3ms
memory: 3428kb

input:

1000
1 1 1 2 4 5 4 7 7 9 9 10 12 13 14 13 14 17 17 18 20 21 22 21 23 25 26 25 28 28 30 31 32 33 34 3...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #34:

score: 0
Accepted
time: 2ms
memory: 3376kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #35:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

1000
1 1 2 3 4 4 6 7 8 9 8 10 11 13 13 14 15 16 17 19 19 21 22 23 24 25 26 27 27 29 29 30 31 32 33 3...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #36:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

1000
1 1 2 1 3 2 1 2 4 8 3 2 9 10 3 14 2 16 18 14 12 4 2 15 19 15 5 21 8 11 10 23 23 13 17 4 8 12 18...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #37:

score: 0
Accepted
time: 2ms
memory: 3536kb

input:

1000
1 1 2 3 4 4 6 5 8 8 10 11 11 13 14 15 16 17 18 19 20 20 22 23 24 24 25 27 27 26 30 31 32 32 33 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #38:

score: 0
Accepted
time: 2ms
memory: 3472kb

input:

1000
1 1 1 3 3 4 6 6 8 9 10 11 12 13 13 15 16 17 18 18 20 21 21 23 23 23 25 27 28 29 30 30 32 33 34 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #39:

score: 0
Accepted
time: 0ms
memory: 3272kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #40:

score: 0
Accepted
time: 2ms
memory: 3272kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #41:

score: 0
Accepted
time: 3ms
memory: 3456kb

input:

1000
1 2 1 3 4 1 1 8 2 7 1 2 4 12 10 6 6 15 12 4 2 11 20 3 4 21 5 11 15 13 27 31 26 10 34 21 29 19 1...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #42:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

1000
1 1 1 2 3 5 6 5 7 9 10 11 11 11 14 15 16 16 18 19 20 20 21 21 24 25 25 26 26 29 30 30 31 33 33 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #43:

score: 0
Accepted
time: 5ms
memory: 3288kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #44:

score: 0
Accepted
time: 5ms
memory: 3540kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #45:

score: 0
Accepted
time: 4ms
memory: 3484kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #46:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

1000
1 1 2 3 3 4 6 7 8 8 9 9 12 13 14 15 15 17 17 18 20 21 22 23 24 24 26 26 28 28 30 29 32 33 34 34...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #47:

score: 0
Accepted
time: 3ms
memory: 3308kb

input:

1000
1 1 2 3 3 4 5 7 6 9 10 11 12 12 14 15 15 17 18 18 19 20 21 23 23 24 25 27 27 29 29 31 32 33 34 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #48:

score: 0
Accepted
time: 3ms
memory: 3508kb

input:

1000
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 17 17 18 20 20 22 23 24 25 25 27 28 29 29 29 32 33 34 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #49:

score: 0
Accepted
time: 3ms
memory: 3508kb

input:

1000
1 1 1 3 4 5 6 6 8 9 10 10 12 12 14 15 16 17 18 19 20 21 22 23 24 25 26 26 28 29 30 30 31 33 34 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #50:

score: 0
Accepted
time: 3ms
memory: 3512kb

input:

1000
1 1 2 3 4 4 6 7 8 9 9 11 12 13 13 15 16 16 18 19 20 20 21 22 23 24 26 26 27 28 30 31 32 32 34 3...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #51:

score: 0
Accepted
time: 3ms
memory: 3572kb

input:

1000
1 1 2 3 4 3 6 6 8 9 10 10 12 12 14 15 16 17 18 18 20 21 21 23 24 24 26 25 28 29 30 31 31 33 34 ...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #52:

score: 0
Accepted
time: 3ms
memory: 3560kb

input:

1000
1 1 2 2 4 5 6 7 8 7 9 11 12 12 14 15 15 17 17 18 20 21 22 22 24 25 26 27 28 28 29 31 31 32 34 3...

output:

POSSIBLE

result:

ok single line: 'POSSIBLE'

Test #53:

score: 0
Accepted
time: 2ms
memory: 3512kb

input:

1000
1 1 2 2 4 5 5 6 8 9 10 11 12 13 14 15 16 17 18 19 20 20 21 23 24 24 26 26 27 29 30 31 32 32 34 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #54:

score: 0
Accepted
time: 2ms
memory: 3420kb

input:

1000
1 1 2 3 4 5 6 7 8 8 10 11 11 13 14 15 15 17 17 19 20 21 22 23 24 24 25 27 28 28 30 31 31 32 33 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #55:

score: 0
Accepted
time: 2ms
memory: 3504kb

input:

1000
1 1 1 3 4 4 5 7 7 9 10 11 12 13 13 15 16 15 17 19 18 21 21 22 23 25 26 27 28 29 30 31 32 32 34 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #56:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

1000
1 1 1 2 3 5 5 7 8 9 9 10 12 13 14 15 16 16 18 18 19 19 22 22 23 24 26 26 26 28 30 30 32 32 33 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #57:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

1000
1 1 1 3 3 5 6 7 8 8 10 11 11 12 12 15 16 17 18 19 20 20 22 22 23 25 25 25 27 28 30 31 32 33 34 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #58:

score: 0
Accepted
time: 1ms
memory: 3460kb

input:

1000
1 1 2 3 4 4 5 6 8 8 8 11 11 13 14 15 16 17 18 18 20 20 22 23 24 25 25 27 28 29 29 30 31 33 34 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #59:

score: 0
Accepted
time: 2ms
memory: 3416kb

input:

1000
1 1 2 3 3 5 5 6 7 9 10 11 11 12 14 15 16 16 18 18 19 21 22 21 23 24 26 27 26 29 30 31 32 32 34 ...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'

Test #60:

score: 0
Accepted
time: 1ms
memory: 3460kb

input:

1000
1 1 1 3 3 4 6 7 8 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 23 25 25 27 28 29 30 30 31 33 33 3...

output:

IMPOSSIBLE

result:

ok single line: 'IMPOSSIBLE'