結果

問題 No.783 門松計画
ユーザー coco18000coco18000
提出日時 2019-02-03 23:00:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 3,020 bytes
コンパイル時間 1,358 ms
コンパイル使用メモリ 151,068 KB
実行使用メモリ 133,692 KB
最終ジャッジ日時 2023-09-30 18:43:00
合計ジャッジ時間 4,104 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
30,012 KB
testcase_01 AC 19 ms
81,320 KB
testcase_02 AC 7 ms
32,080 KB
testcase_03 AC 2 ms
5,416 KB
testcase_04 AC 3 ms
9,796 KB
testcase_05 AC 7 ms
30,168 KB
testcase_06 AC 4 ms
17,996 KB
testcase_07 AC 4 ms
13,632 KB
testcase_08 AC 3 ms
9,520 KB
testcase_09 AC 6 ms
26,020 KB
testcase_10 AC 291 ms
133,564 KB
testcase_11 AC 563 ms
133,684 KB
testcase_12 AC 408 ms
133,692 KB
testcase_13 AC 40 ms
110,936 KB
testcase_14 AC 110 ms
133,096 KB
testcase_15 AC 21 ms
47,080 KB
testcase_16 AC 9 ms
34,304 KB
testcase_17 AC 37 ms
120,328 KB
testcase_18 AC 5 ms
22,108 KB
testcase_19 AC 69 ms
96,268 KB
testcase_20 AC 10 ms
46,552 KB
testcase_21 AC 8 ms
34,464 KB
testcase_22 AC 8 ms
28,632 KB
testcase_23 AC 9 ms
42,392 KB
testcase_24 AC 35 ms
59,376 KB
testcase_25 AC 18 ms
57,096 KB
testcase_26 AC 42 ms
106,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

pll A[55];

//! [i番目に][L[j]を置いて][前回置いたものがL[k]][コストがcとなるような]=最大の長さ
ll dp0[55][55][55][55];
ll dp1[55][55][55][55];

//ll dp0[15][15][15][15];
//ll dp1[15][15][15][15];

int main()
{
    int N, C;
    cin >> N >> C;
    REP(i, N)
    {
        cin >> A[i].first;
    }
    REP(i, N)
    {
        cin >> A[i].second;
    }
    sort(A, A + N, [](pll a, pll b) { return a.first < b.first; });

    //! 奇数番目が小さくなるような門松列
    FOR(i, C + 1, 1)
    {
        REP(j, N)
        {
            REP(k, C + 1)
            {
                REP(m, N)
                {
                    dp0[i][j][m][k] = -1;
                    if (k - A[j].second < 0)
                        continue;
                    if (i % 2 == 0 && A[m].first >= A[j].first)
                        continue;
                    else if (i % 2 == 1 && A[m].first <= A[j].first)
                        continue;
                    REP(n, N)
                    {
                        if (i > 2 && A[n].first == A[j].first)
                            continue;
                        if (dp0[i - 1][m][n][k - A[j].second] == -1)
                            continue;
                        dp0[i][j][m][k] = std::max(dp0[i][j][m][k], dp0[i - 1][m][n][k - A[j].second] + A[j].first);
                    }
                }
            }
        }
    }
    //! 偶数番目が小さくなるような門松列
    FOR(i, C + 1, 1)
    {
        REP(j, N)
        {
            REP(k, C + 1)
            {
                REP(m, N)
                {
                    dp1[i][j][m][k] = -1;
                    if (k - A[j].second < 0)
                        continue;
                    if (i % 2 == 0 && (A[m].first <= A[j].first))
                        continue;
                    else if (i % 2 == 1 && A[m].first >= A[j].first)
                        continue;

                    REP(n, N)
                    {
                        if (i > 2 && A[n].first == A[j].first)
                            continue;
                        if (dp1[i - 1][m][n][k - A[j].second] == -1)
                            continue;
                        dp1[i][j][m][k] = std::max(dp1[i][j][m][k], dp1[i - 1][m][n][k - A[j].second] + A[j].first);
                    }
                }
            }
        }
    }

    ll max = 0;
    FOR(i, C + 1, 3)
    {
        REP(j, N)
        {
            REP(k, C + 1)
            {
                REP(n, N)
                {
                    max = std::max(dp0[i][j][n][k], max);
                    max = std::max(dp1[i][j][n][k], max);
                }
            }
        }
    }
    cout << max << endl;
    return 0;
}
0