Submission #19435465


Source Code Expand

Copy
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {

	static long mod = 1000000007;
	static int inf = Integer.MAX_VALUE-1;
	static long infL = 0xfffffffffffffffL;

	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int n = sc.nextInt();

//		if(n <= 2) {
//			System.out.println("POSSIBLE");
//			return;
//		}

		int[] p = new int[n];
		p[0] = 0;
		for(int i = 1; i < n; i++) {
			p[i] = sc.nextInt()-1;
		}
		int[] x = new int[n];
		for(int i = 0; i < n; i++) {
			x[i] = sc.nextInt();
		}

		boolean[] f = new boolean[n];
		int[][] dp = new int[n][5001];
		for(int i = 0; i < n-1; i++) {
			Arrays.fill(dp[i], -1);
		}

		for(int i = n-1; i > 0; i--) {
			int o = p[i];



			if(!f[i]) {

				if(!f[o]) {
					f[o] = true;
					dp[o][x[i]] = 0;
					dp[o][0] = x[i];
				}else {
					for(int j = 5000; j >= 0; j--) {
						if(dp[o][j] == -1)
							continue;




						int tmp = dp[o][j];
						dp[o][j] = -1;
						if(j <= 5000)
							dp[o][j] = x[i] + tmp;
						if(x[i]+j <= 5000)
							dp[o][x[i]+j] = tmp;
					}
				}

			}else {
				int min = 10000;
				for(int j = 0; j <= x[i]; j++) {
					if(dp[i][j] != -1 && min > dp[i][j] && dp[i][j] <= 5000)
						min = dp[i][j];
				}
				if(min == 10000) {
					System.out.println("IMPOSSIBLE");
					return;
				}
				if(min > 5000) {
					System.out.println("WA");
					return;
				}

				if(!f[o]) {
					f[o] = true;

					dp[o][x[i]] = min;
					dp[o][min] = x[i];

				}else {

					for(int j = 5000; j >= 0; j--) {
						if(dp[o][j] == -1) {
							dp[o][j] = -1;
							continue;
						}

						int tmp = dp[o][j];
						dp[o][j] = -1;
						if(min+j <= 5000)
							dp[o][min+j] = x[i]+tmp;
						if(x[i]+j <= 5000)
							dp[o][x[i]+j] = min+tmp;

					}
				}
			}
		}

		String ans = "IMPOSSIBLE";
		for(int i = 0; i <= x[0]; i++) {
			if(dp[0][i] != -1) {
				ans = "POSSIBLE";
				break;
			}
		}

		System.out.println(ans);

	}

	private static int binarySearch2D(long key, long[][] ps, int ind, int fromIndex, int toIndex) {

		int low = fromIndex;
		int high = toIndex - 1;

		while (low <= high) {
		int mid = (low + high) >>> 1;
		long midVal = ps[mid][ind];

		if (midVal < key)
			low = mid + 1;
		else if (midVal > key)
			high = mid - 1;
		else
			return mid; // key found
		}
		return -(low + 1);  // key not found.
	}
}

class Pair{
	int v;
	long h;

	public Pair(int v, long h) {
		this.v = v;
		this.h = h;
	}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    public FastScanner(InputStream in2) {
		// TODO 自動生成されたコンストラクター・スタブ
	}
	private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}

Submission Info

Submission Time
Task E - Bichrome Tree
User kiruto398
Language Java (OpenJDK 11.0.6)
Score 700
Code Size 4935 Byte
Status AC
Exec Time 171 ms
Memory 66644 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 44
Set Name Test Cases
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
Case Name Status Exec Time Memory
01.txt AC 150 ms 66436 KB
02.txt AC 145 ms 66392 KB
03.txt AC 98 ms 63404 KB
04.txt AC 146 ms 66496 KB
05.txt AC 147 ms 66336 KB
06.txt AC 140 ms 66300 KB
07.txt AC 135 ms 66420 KB
08.txt AC 144 ms 66412 KB
09.txt AC 159 ms 66488 KB
10.txt AC 103 ms 63432 KB
11.txt AC 126 ms 64880 KB
12.txt AC 118 ms 45304 KB
13.txt AC 129 ms 65792 KB
14.txt AC 144 ms 66452 KB
15.txt AC 139 ms 65444 KB
16.txt AC 153 ms 66300 KB
17.txt AC 96 ms 63408 KB
18.txt AC 107 ms 63492 KB
19.txt AC 138 ms 66244 KB
20.txt AC 147 ms 66476 KB
21.txt AC 125 ms 64900 KB
22.txt AC 99 ms 63356 KB
23.txt AC 144 ms 66092 KB
24.txt AC 136 ms 66176 KB
25.txt AC 171 ms 66644 KB
26.txt AC 140 ms 65100 KB
27.txt AC 138 ms 65524 KB
28.txt AC 138 ms 65664 KB
29.txt AC 137 ms 65652 KB
30.txt AC 146 ms 65076 KB
31.txt AC 134 ms 65468 KB
32.txt AC 133 ms 65232 KB
33.txt AC 104 ms 63492 KB
34.txt AC 103 ms 63480 KB
35.txt AC 96 ms 63484 KB
36.txt AC 104 ms 63552 KB
37.txt AC 104 ms 63548 KB
38.txt AC 99 ms 63380 KB
39.txt AC 99 ms 63428 KB
40.txt AC 106 ms 63352 KB
subtask0_0.txt AC 70 ms 32312 KB
subtask0_1.txt AC 78 ms 32616 KB
subtask0_2.txt AC 71 ms 32208 KB
subtask0_3.txt AC 68 ms 32476 KB