Submission #58793


Source Code Expand

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);
	static int N;
	static int[] C, parent;
	static ArrayList<ArrayList<Integer>> child = new ArrayList<ArrayList<Integer>>();

	public static void main(String[] args) {
		N = sc.nextInt();
		C = new int[N];
		parent = new int[N];
		C[0] = sc.nextInt();
		parent[0] = -1;
		for (int i = 0; i < N; ++i) {
			child.add(new ArrayList<Integer>());
		}
		for (int i = 1; i < N; ++i) {
			parent[i] = sc.nextInt() - 1;
			C[i] = sc.nextInt();
			child.get(parent[i]).add(i);
		}
		int shift = 500;
		long[][] dp = new long[N][shift * 2 + 1];
		for (long[] a : dp) {
			Arrays.fill(a, 1L << 50);
		}
		for (int i = N - 1; i >= 0; --i) {
			for (int j = child.get(i).isEmpty() ? 0 : 1; j < dp[0].length; ++j) {
				int to = j - shift;
				long c = Math.abs(C[i] - to);
				for (int n : child.get(i)) {
					c += dp[n][j - 1];
				}
				if (j == 0) {
					dp[i][j] = c;
				} else {
					dp[i][j] = Math.min(c, dp[i][j - 1]);
				}
			}
		}
		long ans = Long.MAX_VALUE;
		for (int i = 0; i < dp[0].length; ++i) {
			ans = Math.min(ans, dp[0][i]);
		}
		System.out.println(ans);

	}
}

Submission Info

Submission Time
Task L - じょうしょうツリー
User tomerun
Language Java (OpenJDK 1.7.0)
Score 50
Code Size 1269 Byte
Status WA
Exec Time 2560 ms
Memory 286256 KB

Judge Result

Set Name Set 01 Set 02
Score / Max Score 50 / 50 0 / 250
Status
AC × 32
AC × 32
WA × 30
TLE × 20
Set Name Test Cases
Set 01 00_sample1.txt, 00_sample2.txt, 01_000_random_N21_C50.txt, 01_001_random_N1_C50.txt, 01_002_random_N17_C50.txt, 01_003_random_N47_C50.txt, 01_004_random_N2_C50.txt, 01_005_random_N50_C50.txt, 01_006_random_N17_C50.txt, 01_007_random_N6_C50.txt, 01_008_random_N4_C50.txt, 01_009_random_N16_C50.txt, 01_010_random_N22_C50.txt, 01_011_random_N24_C50.txt, 01_012_random_N2_C50.txt, 01_013_random_N49_C50.txt, 01_014_random_N24_C50.txt, 01_015_random_N30_C50.txt, 01_016_random_N16_C50.txt, 01_017_random_N33_C50.txt, 01_018_random_N14_C50.txt, 01_019_random_N18_C50.txt, 01_020_random_N21_C50.txt, 01_021_random_N12_C50.txt, 01_022_random_N18_C50.txt, 01_023_random_N27_C50.txt, 01_024_random_N41_C50.txt, 01_025_random_N33_C50.txt, 01_026_random_N3_C50.txt, 01_027_random_N4_C50.txt, 01_028_random_N1_C50.txt, 01_029_random_N11_C50.txt
Set 02 00_sample1.txt, 00_sample2.txt, 01_000_random_N21_C50.txt, 01_001_random_N1_C50.txt, 01_002_random_N17_C50.txt, 01_003_random_N47_C50.txt, 01_004_random_N2_C50.txt, 01_005_random_N50_C50.txt, 01_006_random_N17_C50.txt, 01_007_random_N6_C50.txt, 01_008_random_N4_C50.txt, 01_009_random_N16_C50.txt, 01_010_random_N22_C50.txt, 01_011_random_N24_C50.txt, 01_012_random_N2_C50.txt, 01_013_random_N49_C50.txt, 01_014_random_N24_C50.txt, 01_015_random_N30_C50.txt, 01_016_random_N16_C50.txt, 01_017_random_N33_C50.txt, 01_018_random_N14_C50.txt, 01_019_random_N18_C50.txt, 01_020_random_N21_C50.txt, 01_021_random_N12_C50.txt, 01_022_random_N18_C50.txt, 01_023_random_N27_C50.txt, 01_024_random_N41_C50.txt, 01_025_random_N33_C50.txt, 01_026_random_N3_C50.txt, 01_027_random_N4_C50.txt, 01_028_random_N1_C50.txt, 01_029_random_N11_C50.txt, 02_000_random_N69_C1000000000.txt, 02_001_random_N47_C1000000000.txt, 02_002_random_N67_C1000000000.txt, 02_003_random_N43_C1000000000.txt, 02_004_random_N83_C1000000000.txt, 02_005_random_N89_C1000000000.txt, 02_006_random_N86_C1000000000.txt, 02_007_random_N4_C1000000000.txt, 02_008_random_N94_C1000000000.txt, 02_009_random_N36_C1000000000.txt, 02_010_random_N85_C1000000000.txt, 02_011_random_N17_C1000000000.txt, 02_012_random_N65_C1000000000.txt, 02_013_random_N9_C1000000000.txt, 02_014_random_N68_C1000000000.txt, 02_015_random_N91_C1000000000.txt, 02_016_random_N39_C1000000000.txt, 02_017_random_N57_C1000000000.txt, 02_018_random_N76_C1000000000.txt, 02_019_random_N87_C1000000000.txt, 02_020_random_N36_C1000000000.txt, 02_021_random_N9_C1000000000.txt, 02_022_random_N66_C1000000000.txt, 02_023_random_N67_C1000000000.txt, 02_024_random_N60_C1000000000.txt, 02_025_random_N29_C1000000000.txt, 02_026_random_N92_C1000000000.txt, 02_027_random_N77_C1000000000.txt, 02_028_random_N17_C1000000000.txt, 02_029_random_N28_C1000000000.txt, 02_030_random_1000000000_random_100000.txt, 02_031_random_1000000000_bigd_100000_90000, 02_032_increasing_1000000000_0_random_100000.txt, 02_033_increasing_1000000000_0_bigd_100000_90000, 02_034_increasing_1000000000_30_random_100000.txt, 02_035_increasing_1000000000_30_bigd_100000_90000, 02_036_increasing_1000000000_1000_random_100000.txt, 02_037_increasing_1000000000_1000_bigd_100000_90000, 02_038_increasing_1000000000_10000_random_100000.txt, 02_039_increasing_1000000000_10000_bigd_100000_90000, 02_040_random_1000000000_bigd_100000_50000, 02_041_random_1000000000_bigd_100000_99999, 02_042_random_1000000000_star_100000_4_10000.txt, 02_043_random_1000000000_star_100000_30_3000.txt, 02_044_random_1000000000_longpath-heavyroot_100000_90000, 02_045_increasing_1000000000_50_bigd_100000_50000, 02_046_increasing_1000000000_50_bigd_100000_99999, 02_047_increasing_1000000000_50_star_100000_4_10000.txt, 02_048_increasing_1000000000_50_star_100000_30_3000.txt, 02_049_increasing_1000000000_50_longpath-heavyroot_100000_90000
Case Name Status Exec Time Memory
00_sample1.txt AC 697 ms 20900 KB
00_sample2.txt AC 709 ms 20264 KB
01_000_random_N21_C50.txt AC 604 ms 22320 KB
01_001_random_N1_C50.txt AC 517 ms 20112 KB
01_002_random_N17_C50.txt AC 544 ms 22292 KB
01_003_random_N47_C50.txt AC 705 ms 24536 KB
01_004_random_N2_C50.txt AC 873 ms 20188 KB
01_005_random_N50_C50.txt AC 922 ms 23884 KB
01_006_random_N17_C50.txt AC 725 ms 21192 KB
01_007_random_N6_C50.txt AC 843 ms 20648 KB
01_008_random_N4_C50.txt AC 651 ms 20248 KB
01_009_random_N16_C50.txt AC 487 ms 22668 KB
01_010_random_N22_C50.txt AC 580 ms 23620 KB
01_011_random_N24_C50.txt AC 551 ms 21544 KB
01_012_random_N2_C50.txt AC 622 ms 20136 KB
01_013_random_N49_C50.txt AC 582 ms 24836 KB
01_014_random_N24_C50.txt AC 501 ms 22556 KB
01_015_random_N30_C50.txt AC 864 ms 21932 KB
01_016_random_N16_C50.txt AC 549 ms 21048 KB
01_017_random_N33_C50.txt AC 510 ms 24156 KB
01_018_random_N14_C50.txt AC 541 ms 21828 KB
01_019_random_N18_C50.txt AC 507 ms 22156 KB
01_020_random_N21_C50.txt AC 687 ms 22824 KB
01_021_random_N12_C50.txt AC 465 ms 21676 KB
01_022_random_N18_C50.txt AC 502 ms 22616 KB
01_023_random_N27_C50.txt AC 533 ms 22828 KB
01_024_random_N41_C50.txt AC 728 ms 24564 KB
01_025_random_N33_C50.txt AC 496 ms 23380 KB
01_026_random_N3_C50.txt AC 727 ms 20120 KB
01_027_random_N4_C50.txt AC 763 ms 20276 KB
01_028_random_N1_C50.txt AC 535 ms 20128 KB
01_029_random_N11_C50.txt AC 1255 ms 21660 KB
02_000_random_N69_C1000000000.txt WA 605 ms 25600 KB
02_001_random_N47_C1000000000.txt WA 611 ms 23708 KB
02_002_random_N67_C1000000000.txt WA 598 ms 25664 KB
02_003_random_N43_C1000000000.txt WA 813 ms 23840 KB
02_004_random_N83_C1000000000.txt WA 563 ms 26228 KB
02_005_random_N89_C1000000000.txt WA 572 ms 26476 KB
02_006_random_N86_C1000000000.txt WA 698 ms 26424 KB
02_007_random_N4_C1000000000.txt WA 767 ms 20264 KB
02_008_random_N94_C1000000000.txt WA 787 ms 26468 KB
02_009_random_N36_C1000000000.txt WA 710 ms 24224 KB
02_010_random_N85_C1000000000.txt WA 589 ms 27428 KB
02_011_random_N17_C1000000000.txt WA 508 ms 22688 KB
02_012_random_N65_C1000000000.txt WA 575 ms 25232 KB
02_013_random_N9_C1000000000.txt WA 506 ms 21292 KB
02_014_random_N68_C1000000000.txt WA 547 ms 25596 KB
02_015_random_N91_C1000000000.txt WA 585 ms 26428 KB
02_016_random_N39_C1000000000.txt WA 553 ms 23344 KB
02_017_random_N57_C1000000000.txt WA 597 ms 24972 KB
02_018_random_N76_C1000000000.txt WA 560 ms 25796 KB
02_019_random_N87_C1000000000.txt WA 847 ms 26252 KB
02_020_random_N36_C1000000000.txt WA 615 ms 24300 KB
02_021_random_N9_C1000000000.txt WA 785 ms 20780 KB
02_022_random_N66_C1000000000.txt WA 685 ms 25484 KB
02_023_random_N67_C1000000000.txt WA 599 ms 25644 KB
02_024_random_N60_C1000000000.txt WA 531 ms 25224 KB
02_025_random_N29_C1000000000.txt WA 553 ms 23072 KB
02_026_random_N92_C1000000000.txt WA 526 ms 26480 KB
02_027_random_N77_C1000000000.txt WA 519 ms 27484 KB
02_028_random_N17_C1000000000.txt WA 734 ms 21152 KB
02_029_random_N28_C1000000000.txt WA 577 ms 23720 KB
02_030_random_1000000000_random_100000.txt TLE 2166 ms 285352 KB
02_031_random_1000000000_bigd_100000_90000 TLE 2560 ms 286256 KB
02_032_increasing_1000000000_0_random_100000.txt TLE 2240 ms 283960 KB
02_033_increasing_1000000000_0_bigd_100000_90000 TLE 2210 ms 284608 KB
02_034_increasing_1000000000_30_random_100000.txt TLE 2073 ms 282248 KB
02_035_increasing_1000000000_30_bigd_100000_90000 TLE 2204 ms 283464 KB
02_036_increasing_1000000000_1000_random_100000.txt TLE 2328 ms 284456 KB
02_037_increasing_1000000000_1000_bigd_100000_90000 TLE 2308 ms 285352 KB
02_038_increasing_1000000000_10000_random_100000.txt TLE 2115 ms 280296 KB
02_039_increasing_1000000000_10000_bigd_100000_90000 TLE 2128 ms 284796 KB
02_040_random_1000000000_bigd_100000_50000 TLE 2124 ms 285488 KB
02_041_random_1000000000_bigd_100000_99999 TLE 2069 ms 284116 KB
02_042_random_1000000000_star_100000_4_10000.txt TLE 2155 ms 285348 KB
02_043_random_1000000000_star_100000_30_3000.txt TLE 2145 ms 286200 KB
02_044_random_1000000000_longpath-heavyroot_100000_90000 TLE 2144 ms 284544 KB
02_045_increasing_1000000000_50_bigd_100000_50000 TLE 2127 ms 282716 KB
02_046_increasing_1000000000_50_bigd_100000_99999 TLE 2106 ms 285288 KB
02_047_increasing_1000000000_50_star_100000_4_10000.txt TLE 2101 ms 284252 KB
02_048_increasing_1000000000_50_star_100000_30_3000.txt TLE 2053 ms 283400 KB
02_049_increasing_1000000000_50_longpath-heavyroot_100000_90000 TLE 2083 ms 282456 KB