結果

問題 No.1858 Gorgeous Knapsack
ユーザー kacho65535kacho65535
提出日時 2023-02-14 19:51:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 2,839 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 207,160 KB
実行使用メモリ 199,480 KB
最終ジャッジ日時 2023-09-24 07:48:32
合計ジャッジ時間 6,768 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 55 ms
45,704 KB
testcase_05 AC 13 ms
13,080 KB
testcase_06 AC 67 ms
56,812 KB
testcase_07 AC 46 ms
40,352 KB
testcase_08 AC 95 ms
80,100 KB
testcase_09 AC 160 ms
136,952 KB
testcase_10 AC 60 ms
51,852 KB
testcase_11 AC 88 ms
74,928 KB
testcase_12 AC 131 ms
111,528 KB
testcase_13 AC 32 ms
28,052 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 23 ms
19,916 KB
testcase_25 AC 120 ms
103,872 KB
testcase_26 AC 90 ms
76,840 KB
testcase_27 AC 88 ms
74,608 KB
testcase_28 AC 70 ms
62,180 KB
testcase_29 AC 13 ms
13,240 KB
testcase_30 AC 19 ms
17,560 KB
testcase_31 AC 16 ms
15,260 KB
testcase_32 AC 14 ms
13,204 KB
testcase_33 AC 24 ms
21,832 KB
testcase_34 AC 258 ms
199,480 KB
testcase_35 AC 75 ms
23,504 KB
testcase_36 AC 251 ms
199,212 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 4 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 75 ms
23,748 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