結果

問題 No.10 +か×か
ユーザー GrenacheGrenache
提出日時 2016-06-01 00:11:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 165 ms / 5,000 ms
コード長 1,137 bytes
コンパイル時間 4,374 ms
コンパイル使用メモリ 76,504 KB
実行使用メモリ 60,720 KB
最終ジャッジ日時 2023-08-21 06:19:31
合計ジャッジ時間 5,973 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,756 KB
testcase_01 AC 123 ms
56,224 KB
testcase_02 AC 123 ms
55,724 KB
testcase_03 AC 165 ms
60,416 KB
testcase_04 AC 159 ms
60,504 KB
testcase_05 AC 129 ms
55,748 KB
testcase_06 AC 165 ms
60,468 KB
testcase_07 AC 165 ms
60,720 KB
testcase_08 AC 143 ms
55,940 KB
testcase_09 AC 153 ms
57,928 KB
testcase_10 AC 125 ms
55,772 KB
testcase_11 AC 125 ms
56,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder10 {

    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();
        }

        boolean[][] dp = new boolean[n][total + 1];
        dp[n - 1][total] = true;

        for (int j = n - 1; j > 0; j--) {
            for (int i = 1; i <= total; i++) {
        		if (i * a[j] <= total && dp[j][i * a[j]]) {
        			dp[j - 1][i] = true;
        		}
        		if (i + a[j] <= total && dp[j][i + a[j]]) {
        			dp[j - 1][i] = true;
        		}
        	}
        }

        String ret = "";
        int j = a[0];
        for (int i = 1; i < n; i++) {
        	if (j + a[i] <= total && dp[i][j + a[i]]) {
        		ret += "+";
        		j += a[i];
        	} else if (j * a[i] <= total && dp[i][j * a[i]]){
        		ret += "*";
        		j *= a[i];
        	} else {
        		System.out.println("error");
        	}
        }

        System.out.println(ret);

        sc.close();
    }
}
0