結果

問題 No.10 +か×か
コンテスト
ユーザー Grenache
提出日時 2016-05-31 23:33:27
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 1,235 ms / 5,000 ms
コード長 1,306 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,641 ms
コンパイル使用メモリ 85,064 KB
実行使用メモリ 453,116 KB
最終ジャッジ日時 2026-05-25 09:09:38
合計ジャッジ時間 10,018 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

        List<Map<Integer, String>> dp = new ArrayList<>();
        for (int i = 0; i < n; i++) {
        	dp.add(new HashMap<>());
        }
        dp.get(0).put(a[0], "");

        for (int j = 1; j < n; j++) {
            for (int i = 1; i <= total; i++) {
        		if (!dp.get(j - 1).containsKey(i)) {
        			continue;
        		}

        		if (i * a[j] <= total) {
        			String tmp = dp.get(j - 1).get(i) + '*';
        			if (dp.get(j).containsKey(i * a[j]) && dp.get(j).get(i * a[j]).compareTo(tmp) > 0) {
        			} else {
        				dp.get(j).put(i * a[j], tmp);
        			}
        		}
        		if (i + a[j] <= total) {
        			String tmp = dp.get(j - 1).get(i) + '+';
        			if (dp.get(j).containsKey(i + a[j]) && dp.get(j).get(i + a[j]).compareTo(tmp) > 0) {
        			} else {
        				dp.get(j).put(i + a[j], tmp);
        			}
        		}
        	}
        }

        System.out.println(dp.get(n - 1).get(total));

        sc.close();
    }
}
0