結果

問題 No.1858 Gorgeous Knapsack
ユーザー kacho65535kacho65535
提出日時 2023-02-14 19:52:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 2,872 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 207,384 KB
実行使用メモリ 199,340 KB
最終ジャッジ日時 2023-09-24 07:48:38
合計ジャッジ時間 5,575 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 55 ms
45,712 KB
testcase_05 AC 14 ms
12,880 KB
testcase_06 AC 69 ms
56,840 KB
testcase_07 AC 47 ms
40,256 KB
testcase_08 AC 96 ms
80,092 KB
testcase_09 AC 164 ms
137,040 KB
testcase_10 AC 63 ms
52,044 KB
testcase_11 AC 90 ms
74,924 KB
testcase_12 AC 133 ms
111,676 KB
testcase_13 AC 32 ms
27,988 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 23 ms
20,152 KB
testcase_25 AC 122 ms
103,936 KB
testcase_26 AC 92 ms
76,688 KB
testcase_27 AC 89 ms
74,632 KB
testcase_28 AC 72 ms
62,016 KB
testcase_29 AC 13 ms
13,156 KB
testcase_30 AC 19 ms
17,588 KB
testcase_31 AC 16 ms
15,280 KB
testcase_32 AC 13 ms
13,224 KB
testcase_33 AC 24 ms
21,792 KB
testcase_34 AC 254 ms
199,264 KB
testcase_35 AC 64 ms
23,492 KB
testcase_36 AC 256 ms
199,340 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 4 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 64 ms
23,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define BEGIN_STACK_EXTEND(size)                                                                                  \
    void *stack_extend_memory_ = malloc(size);                                                                    \
    void *stack_extend_origin_memory_;                                                                            \
    char *stack_extend_dummy_memory_ = (char *)alloca((1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \
    *stack_extend_dummy_memory_ = 0;                                                                              \
    asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp"                                                             \
                 : "=b"(stack_extend_origin_memory_)                                                              \
                 : "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND                                                 \
    asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_)); \
    free(stack_extend_memory_);
#define mod 1000000007ll
#define loop(i, n) for (int i = 0; i < n; i++)
#define flagcount(bit) __builtin_popcount(bit)
#define flag(x) (1ll << x)
#define flagadd(bit, x) bit |= flag(x)
#define flagpop(bit, x) bit &= ~flag(x)
#define flagon(bit, i) bit &flag(i)
#define flagoff(bit, i) !(bit & (1ll << i))
#define all(v) v.begin(), v.end()
#define putout(a) cout << a << '\n'
#define Sum(v) accumulate(all(v), 0ll)
template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b; // aをbで更新
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b; // aをbで更新
        return true;
    }
    return false;
}
void AS(ll X, ll L, ll R) { assert(L <= X && X <= R); }
ll dp[5010][5010];
int main()
{
    // cout << fixed << setprecision(10);
    /*
    最小値を決めうったときの解が求まればよい。
    美しさで逆順ソートされているとする。終わり
    dp[i][j]:i番目まで見てjを作れるか?
    */
    ll N, M;
    cin >> N >> M;
    vector<pair<ll, ll>> item(N);
    loop(i, N)
    {
        ll v, w;
        cin >> v >> w;
        item[i] = make_pair(v, w);
    }
    sort(all(item));
    reverse(all(item));
    vector<ll> V(N), W(N);
    loop(i, N)
    {
        V[i] = item[i].first;
        W[i] = item[i].second;
    }
    ll ans = 0;
    loop(i, N)
    {
        loop(j, M + 1)
        {
            chmax(dp[i + 1][j], dp[i][j]);
            if (j >= W[i])
            {
                chmax(dp[i + 1][j], dp[i][j - W[i]] + V[i]);
            }
            // 外に出してみた
            chmax(ans, dp[i + 1][j] * V[i]);
        }
    }
    putout(ans);
    return 0;
}
0