結果

問題 No.783 門松計画
ユーザー simansiman
提出日時 2023-07-05 19:05:10
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 4,061 ms
コンパイル使用メモリ 105,900 KB
実行使用メモリ 4,556 KB
最終ジャッジ日時 2023-09-26 19:28:06
合計ジャッジ時間 6,672 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,556 KB
testcase_02 AC 2 ms
4,488 KB
testcase_03 AC 2 ms
4,512 KB
testcase_04 AC 2 ms
4,480 KB
testcase_05 AC 2 ms
4,484 KB
testcase_06 AC 2 ms
4,468 KB
testcase_07 AC 3 ms
4,416 KB
testcase_08 AC 3 ms
4,424 KB
testcase_09 AC 2 ms
4,452 KB
testcase_10 AC 5 ms
4,512 KB
testcase_11 AC 19 ms
4,452 KB
testcase_12 AC 8 ms
4,408 KB
testcase_13 AC 3 ms
4,424 KB
testcase_14 AC 3 ms
4,484 KB
testcase_15 AC 2 ms
4,400 KB
testcase_16 AC 2 ms
4,436 KB
testcase_17 AC 2 ms
4,548 KB
testcase_18 AC 2 ms
4,540 KB
testcase_19 AC 2 ms
4,416 KB
testcase_20 AC 2 ms
4,396 KB
testcase_21 AC 2 ms
4,540 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,424 KB
testcase_24 AC 2 ms
4,476 KB
testcase_25 AC 2 ms
4,428 KB
testcase_26 AC 3 ms
4,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, C;
  cin >> N >> C;
  int L[N];
  int W[N];
  int max_L = 0;

  for (int i = 0; i < N; ++i) {
    cin >> L[i];

    max_L = max(max_L, L[i]);
  }
  for (int i = 0; i < N; ++i) {
    cin >> W[i];
  }

  int dp_up[51][51][51];
  int dp_down[51][51][51];
  memset(dp_up, -1, sizeof(dp_up));
  memset(dp_down, -1, sizeof(dp_down));

  for (int i = 0; i < N; ++i) {
    int l1 = L[i];
    int w1 = W[i];
    if (w1 > C) continue;

    for (int j = 0; j < N; ++j) {
      int l2 = L[j];
      int w2 = W[j];
      int w = w1 + w2;
      if (w > C) continue;
      if (l1 == l2) continue;

      if (l1 < l2) {
        dp_up[w][l1][l2] = l1 + l2;
      } else {
        dp_down[w][l1][l2] = l1 + l2;
      }
    }
  }

  int ans = 0;

  for (int c = 0; c < C; ++c) {
    for (int s1 = 1; s1 <= max_L; ++s1) {
      for (int s2 = 1; s2 <= max_L; ++s2) {
        if (dp_up[c][s1][s2] != -1) {
          for (int i = 0; i < N; ++i) {
            int s3 = L[i];
            int w = W[i];
            int nc = c + w;
            if (s2 <= s3) continue;
            if (s1 == s3) continue;
            if (nc > C) continue;

            dp_down[nc][s2][s3] = max(dp_down[nc][s2][s3], dp_up[c][s1][s2] + s3);
            ans = max(ans, dp_down[nc][s2][s3]);
          }
        }

        if (dp_down[c][s1][s2] != -1) {
          for (int i = 0; i < N; ++i) {
            int s3 = L[i];
            int w = W[i];
            int nc = c + w;
            if (s2 >= s3) continue;
            if (s1 == s3) continue;
            if (nc > C) continue;

            dp_up[nc][s2][s3] = max(dp_up[nc][s2][s3], dp_down[c][s1][s2] + s3);
            // fprintf(stderr, "(%d -> %d): %d\n", s2, s3, dp_down[c][s1][s2] + s3);
            ans = max(ans, dp_up[nc][s2][s3]);
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0