結果

問題 No.417 チューリップバブル
ユーザー 37zigen37zigen
提出日時 2016-08-14 00:45:24
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,369 ms / 2,000 ms
コード長 4,692 bytes
コンパイル時間 3,748 ms
コンパイル使用メモリ 76,888 KB
実行使用メモリ 59,140 KB
最終ジャッジ日時 2023-08-07 13:46:13
合計ジャッジ時間 24,472 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
48,980 KB
testcase_01 AC 44 ms
49,096 KB
testcase_02 AC 43 ms
48,896 KB
testcase_03 AC 44 ms
49,008 KB
testcase_04 AC 43 ms
48,888 KB
testcase_05 AC 43 ms
49,308 KB
testcase_06 AC 45 ms
49,224 KB
testcase_07 AC 46 ms
49,060 KB
testcase_08 AC 105 ms
53,004 KB
testcase_09 AC 198 ms
54,292 KB
testcase_10 AC 238 ms
54,932 KB
testcase_11 AC 385 ms
55,024 KB
testcase_12 AC 439 ms
55,100 KB
testcase_13 AC 196 ms
54,836 KB
testcase_14 AC 583 ms
55,428 KB
testcase_15 AC 212 ms
54,896 KB
testcase_16 AC 226 ms
54,792 KB
testcase_17 AC 331 ms
55,276 KB
testcase_18 AC 427 ms
54,848 KB
testcase_19 AC 331 ms
55,288 KB
testcase_20 AC 776 ms
57,100 KB
testcase_21 AC 785 ms
56,892 KB
testcase_22 AC 669 ms
57,596 KB
testcase_23 AC 753 ms
59,140 KB
testcase_24 AC 47 ms
49,080 KB
testcase_25 AC 727 ms
58,024 KB
testcase_26 AC 386 ms
55,476 KB
testcase_27 AC 633 ms
57,540 KB
testcase_28 AC 767 ms
56,720 KB
testcase_29 AC 686 ms
57,784 KB
testcase_30 AC 964 ms
57,244 KB
testcase_31 AC 969 ms
57,884 KB
testcase_32 AC 63 ms
50,952 KB
testcase_33 AC 162 ms
53,940 KB
testcase_34 AC 375 ms
54,876 KB
testcase_35 AC 1,305 ms
57,020 KB
testcase_36 AC 1,369 ms
56,916 KB
testcase_37 AC 1,258 ms
57,276 KB
testcase_38 AC 1,153 ms
57,544 KB
testcase_39 AC 1,211 ms
57,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class チューリップバブル {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	class Pair {
		long u;
		int t;

		Pair(long u, int t) {
			this.u = u;
			this.t = t;
		}
	}

	void solver() {

		int n = ni();// 村の数
		int m = ni();// 残り時間

		long[] u = new long[n];
		for (int i = 0; i < n; i++) {
			u[i] = nl();// 税収
		}

		ArrayList<Edge>[] e = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			e[i] = new ArrayList<>();
		}
		for (int i = 0; i < n - 1; i++) {
			int a = ni();
			int b = ni();
			int c = ni();
			e[a].add(new Edge(c, a, b));
			e[b].add(new Edge(c, b, a));
		}

		long[] list = dfs(-1, 0, e, u, m);
		long ans = 0;
		for (int i = 0; i <= m; i++) {
			ans = Math.max(ans, list[i]);
		}

		out.println(ans);
	}

	long[] dfs(int p, int cur, ArrayList<Edge>[] e, long[] u, int m) {
		if (m < 0) {
			return new long[0];
		}
		long[] now = new long[m + 1];
		Arrays.fill(now, -1);
		now[0] = u[cur];
		for (int i = 0; i < e[cur].size(); i++) {
			Edge v = e[cur].get(i);
			if (v.to != p) {
				long[] d = dfs(cur, v.to, e, u, m - 2 * v.time);
				long[] next = Arrays.copyOf(now, now.length);
				for (int j = 0; j <= m; j++) {
					for (int k = 0; k <= m; k++) {
						if (2 * v.time + j + k <= m && d[j] != -1 && now[k] != -1) {
							if (next[2 * v.time + j + k] < d[j] + now[k]) {
								next[2 * v.time + j + k] = d[j] + now[k];
							}
						}
					}
				}
				now = next;
			}
		}
		return now;
	}

	static class Edge {
		int time;
		int from;
		int to;

		Edge(int time, int from, int to) {
			this.time = time;
			this.from = from;
			this.to = to;
		}
	}

	class State {
		long u;
		int t;

		State(long u, int t) {
			this.u = u;
			this.t = t;
		}
	}

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		long t = System.currentTimeMillis();
		new チューリップバブル().solver();
		System.err.println(System.currentTimeMillis() - t);
		out.flush();
	}

	static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}
}
0