結果

問題 No.10 +か×か
ユーザー Grenache
提出日時 2016-06-01 00:11:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 187 ms / 5,000 ms
コード長 1,137 bytes
コンパイル時間 3,277 ms
コンパイル使用メモリ 79,440 KB
実行使用メモリ 47,764 KB
最終ジャッジ日時 2024-12-14 15:36:37
合計ジャッジ時間 5,686 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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