結果

問題 No.417 チューリップバブル
ユーザー 37zigen37zigen
提出日時 2016-08-14 01:01:10
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 4,545 bytes
コンパイル時間 3,859 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 43,064 KB
最終ジャッジ日時 2024-04-25 07:33:24
合計ジャッジ時間 14,629 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 52 ms
36,976 KB
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 53 ms
36,964 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 RE -
testcase_33 WA -
testcase_34 RE -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
		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 = now;
				for (int j = 0; 2 * v.time + j <= m; j++) {
					for (int k = 0; 2 * v.time + j + k <= m; k++) {
						if (d[j] != 0 && now[k] != 0) {
							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;
		}
	}

	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