結果

問題 No.10 +か×か
ユーザー ぴろずぴろず
提出日時 2014-12-20 23:34:14
言語 Java19
(openjdk 21)
結果
AC  
実行時間 231 ms / 5,000 ms
コード長 911 bytes
コンパイル時間 2,089 ms
コンパイル使用メモリ 74,380 KB
実行使用メモリ 105,300 KB
最終ジャッジ日時 2023-08-21 06:09:42
合計ジャッジ時間 4,793 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,204 KB
testcase_01 AC 120 ms
55,888 KB
testcase_02 AC 124 ms
55,644 KB
testcase_03 AC 226 ms
105,300 KB
testcase_04 AC 204 ms
84,872 KB
testcase_05 AC 130 ms
55,800 KB
testcase_06 AC 231 ms
105,044 KB
testcase_07 AC 206 ms
85,116 KB
testcase_08 AC 165 ms
66,924 KB
testcase_09 AC 171 ms
66,912 KB
testcase_10 AC 126 ms
55,544 KB
testcase_11 AC 122 ms
55,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no010;

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int total = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = sc.nextInt();
		}
		long[][] dp = new long[n][total+1];
		for(int i=0;i<n;i++) {
			Arrays.fill(dp[i], 1L<<60);
		}
		dp[0][a[0]] = 0;
		for(int i=0;i<n-1;i++) {
			for(int j=0;j<=total+1;j++) {
				if (j + a[i+1] <= total) {
					dp[i+1][j+a[i+1]] = Math.min(dp[i+1][j+a[i+1]],dp[i][j] * 2);
				}
				if (j * a[i+1] <= total) {
					dp[i+1][j*a[i+1]] = Math.min(dp[i+1][j*a[i+1]],dp[i][j] * 2 + 1);
				}
			}
		}
		StringBuilder sb = new StringBuilder();
		long ans = dp[n-1][total];
		for(int i=0;i<n-1;i++) {
			sb.append(ans % 2 == 0 ? '+' : '*');
			ans >>= 1;
		}
		sb.reverse();
		System.out.println(sb.toString());
	}
}
0