結果

問題 No.2591 安上がりな括弧列
ユーザー 👑 chro_96chro_96
提出日時 2023-12-19 23:21:32
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 30,848 KB
実行使用メモリ 17,564 KB
最終ジャッジ日時 2023-12-19 23:21:33
合計ジャッジ時間 1,462 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
17,564 KB
testcase_01 AC 6 ms
17,564 KB
testcase_02 AC 6 ms
17,564 KB
testcase_03 AC 6 ms
17,564 KB
testcase_04 AC 5 ms
17,564 KB
testcase_05 AC 6 ms
17,564 KB
testcase_06 AC 5 ms
17,564 KB
testcase_07 AC 6 ms
17,564 KB
testcase_08 AC 6 ms
17,564 KB
testcase_09 AC 5 ms
17,564 KB
testcase_10 AC 6 ms
17,564 KB
testcase_11 AC 12 ms
17,564 KB
testcase_12 AC 11 ms
17,564 KB
testcase_13 AC 12 ms
17,564 KB
testcase_14 AC 12 ms
17,564 KB
testcase_15 AC 12 ms
17,564 KB
testcase_16 AC 12 ms
17,564 KB
testcase_17 AC 8 ms
17,564 KB
testcase_18 AC 9 ms
17,564 KB
testcase_19 AC 9 ms
17,564 KB
testcase_20 AC 12 ms
17,564 KB
testcase_21 AC 12 ms
17,564 KB
testcase_22 AC 12 ms
17,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define INF 2000000000000LL

int main () {
  int n = 0;
  char s[2001] = "";
  long long a[2000] = {};
  
  int res = 0;
  
  long long dp[2001][1001] = {};
  
  res = scanf("%d", &n);
  res = scanf("%s", s);
  for (int i = 0; i < 2*n; i++) {
    res = scanf("%lld", a+i);
  }
  
  for (int i = 0; i < n; i++) {
    dp[0][i+1] = INF;
  }
  
  for (int i = 0; i < 2*n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i+1][j] = INF;
    }
    for (int j = 0; j < n; j++) {
      if (s[i] == '(' && dp[i][j] < dp[i+1][j+1]) {
        dp[i+1][j+1] = dp[i][j];
      } else if (s[i] == ')' && dp[i][j]+a[i] < dp[i+1][j+1]) {
        dp[i+1][j+1] = dp[i][j]+a[i];
      }
    }
    for (int j = 0; j < n; j++) {
      if (s[i] == '(' && dp[i][j+1]+a[i] < dp[i+1][j]) {
        dp[i+1][j] = dp[i][j+1]+a[i];
      } else if (s[i] == ')' && dp[i][j+1] < dp[i+1][j]) {
        dp[i+1][j] = dp[i][j+1];
      }
    }
  }
  
  printf("%lld\n", dp[2*n][0]);
  return 0;
}
0