結果

問題 No.2591 安上がりな括弧列
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2023-12-19 17:41:54
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,029 ms / 2,000 ms
コード長 568 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2023-12-19 17:42:09
合計ジャッジ時間 12,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
16,256 KB
testcase_01 AC 90 ms
16,256 KB
testcase_02 AC 85 ms
16,256 KB
testcase_03 AC 87 ms
16,256 KB
testcase_04 AC 85 ms
16,256 KB
testcase_05 AC 86 ms
16,256 KB
testcase_06 AC 106 ms
16,256 KB
testcase_07 AC 86 ms
16,256 KB
testcase_08 AC 87 ms
16,256 KB
testcase_09 AC 93 ms
16,384 KB
testcase_10 AC 91 ms
16,256 KB
testcase_11 AC 1,026 ms
16,512 KB
testcase_12 AC 1,026 ms
16,512 KB
testcase_13 AC 992 ms
16,512 KB
testcase_14 AC 1,029 ms
16,512 KB
testcase_15 AC 1,021 ms
16,512 KB
testcase_16 AC 1,000 ms
16,512 KB
testcase_17 AC 314 ms
16,384 KB
testcase_18 AC 520 ms
16,512 KB
testcase_19 AC 434 ms
16,512 KB
testcase_20 AC 1,024 ms
16,512 KB
testcase_21 AC 995 ms
16,512 KB
testcase_22 AC 1,021 ms
16,512 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def _min(a,b);a < b ? a : b; end
N = gets.to_i
S = gets.chomp.chars
A = gets.split.map(&:to_i)
dp = Array.new(N + 1, Float::INFINITY)
ndp = Array.new(N + 1, Float::INFINITY)
dp[0] = 0
S.zip(A) do |s,a|
  (0 .. N).each do |d|
    if s == ?(
      ndp[d + 1] = _min(dp[d], ndp[d + 1]) if d + 1 <= N
      ndp[d - 1] = _min(dp[d] + a, ndp[d - 1]) if d - 1 >= 0      
    else
      ndp[d + 1] = _min(dp[d] + a, ndp[d + 1]) if d + 1 <= N
      ndp[d - 1] = _min(dp[d], ndp[d - 1]) if d - 1 >= 0
    end
  end
  ndp, dp = dp, ndp
  ndp.fill(Float::INFINITY)
end
puts dp[0]
0