結果

問題 No.10 +か×か
ユーザー vjudge1
提出日時 2025-09-05 10:58:40
言語 Crystal
(1.14.0)
結果
AC  
実行時間 429 ms / 5,000 ms
コード長 833 bytes
コンパイル時間 13,615 ms
コンパイル使用メモリ 309,664 KB
実行使用メモリ 220,752 KB
最終ジャッジ日時 2025-09-05 10:58:56
合計ジャッジ時間 14,727 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

n = gets.not_nil!.to_i
t = gets.not_nil!.to_i
a = gets.not_nil!.split.map(&.to_i)

# Initialize DP table with "!" (invalid marker)
dp = Array(Array(String)).new(n) { Array(String).new(t + 1, "!") }

# Base case
dp[0][a[0]] = ""

# Fill DP table
(0...n - 1).each do |i|
  (0..t).each do |j|
    next if dp[i][j] == "!"  # Skip invalid states
    
    # Addition operation
    if j + a[i + 1] <= t
      candidate = dp[i][j] + "+"
      if dp[i + 1][j + a[i + 1]] == "!" || candidate > dp[i + 1][j + a[i + 1]]
        dp[i + 1][j + a[i + 1]] = candidate
      end
    end
    
    # Multiplication operation
    if j * a[i + 1] <= t
      candidate = dp[i][j] + "*"
      if dp[i + 1][j * a[i + 1]] == "!" || candidate > dp[i + 1][j * a[i + 1]]
        dp[i + 1][j * a[i + 1]] = candidate
      end
    end
  end
end

puts dp[n - 1][t]
0