結果

問題 No.783 門松計画
ユーザー MisterMister
提出日時 2020-08-05 16:35:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,393 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 80,392 KB
最終ジャッジ日時 2025-01-12 14:57:33
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

constexpr int L = 50;
constexpr int INF = 1 << 30;

bool judge(int a, int b, int c) {
    return ((a < b && b > c) || (a > b && b < c)) && a != c;
}

void solve() {
    int n, c;
    std::cin >> n >> c;

    std::vector<std::pair<int, int>> ps(n);
    for (auto& [l, w] : ps) {
        std::cin >> l;
        --l;
    }
    for (auto& [l, w] : ps) std::cin >> w;

    auto dp = vec(L, vec(L, vec(c + 1, -INF)));
    for (auto [l1, w1] : ps) {
        for (auto [l2, w2] : ps) {
            if (l1 == l2 || w1 + w2 > c) continue;
            dp[l1][l2][w1 + w2] = std::max(dp[l1][l2][w1 + w2], l1 + l2 + 2);
        }
    }

    int ans = 0;
    for (int p = 0; p <= c; ++p) {
        for (int l1 = 0; l1 < L; ++l1) {
            for (int l2 = 0; l2 < L; ++l2) {
                if (dp[l1][l2][p] < 0) continue;

                for (auto [l, w] : ps) {
                    if (!judge(l1, l2, l) || p + w > c) continue;

                    dp[l2][l][p + w] = std::max(dp[l2][l][p + w], dp[l1][l2][p] + l + 1);
                    ans = std::max(ans, dp[l2][l][p + w]);
                }
            }
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0