結果

問題 No.1858 Gorgeous Knapsack
ユーザー simansiman
提出日時 2022-02-27 20:15:15
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,050 bytes
コンパイル時間 7,024 ms
コンパイル使用メモリ 106,320 KB
実行使用メモリ 199,792 KB
最終ジャッジ日時 2023-09-19 07:35:14
合計ジャッジ時間 16,149 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
199,496 KB
testcase_01 AC 107 ms
199,576 KB
testcase_02 AC 99 ms
199,560 KB
testcase_03 AC 99 ms
199,548 KB
testcase_04 AC 107 ms
199,616 KB
testcase_05 AC 101 ms
199,580 KB
testcase_06 AC 108 ms
199,704 KB
testcase_07 AC 105 ms
199,664 KB
testcase_08 AC 112 ms
199,660 KB
testcase_09 AC 120 ms
199,684 KB
testcase_10 AC 107 ms
199,792 KB
testcase_11 AC 110 ms
199,672 KB
testcase_12 AC 116 ms
199,768 KB
testcase_13 AC 103 ms
199,684 KB
testcase_14 AC 98 ms
199,584 KB
testcase_15 AC 99 ms
199,496 KB
testcase_16 AC 99 ms
199,584 KB
testcase_17 AC 99 ms
199,556 KB
testcase_18 AC 104 ms
199,496 KB
testcase_19 AC 95 ms
199,628 KB
testcase_20 AC 95 ms
199,500 KB
testcase_21 AC 94 ms
199,596 KB
testcase_22 AC 91 ms
199,504 KB
testcase_23 AC 91 ms
199,608 KB
testcase_24 AC 91 ms
199,604 KB
testcase_25 AC 102 ms
199,680 KB
testcase_26 AC 99 ms
199,644 KB
testcase_27 AC 99 ms
199,728 KB
testcase_28 AC 97 ms
199,624 KB
testcase_29 AC 89 ms
199,664 KB
testcase_30 AC 89 ms
199,680 KB
testcase_31 AC 90 ms
199,732 KB
testcase_32 AC 90 ms
199,700 KB
testcase_33 AC 90 ms
199,620 KB
testcase_34 AC 138 ms
199,768 KB
testcase_35 AC 117 ms
199,696 KB
testcase_36 AC 140 ms
199,676 KB
testcase_37 AC 87 ms
199,492 KB
testcase_38 AC 89 ms
199,672 KB
testcase_39 AC 87 ms
199,576 KB
testcase_40 AC 116 ms
199,756 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;

struct Jewelry {
  ll v;
  ll w;

  Jewelry(ll v = -1, ll w = -1) {
    this->v = v;
    this->w = w;
  }

  bool operator<(const Jewelry &n) const {
    return v > n.v;
  }
};

ll dp[5010][5010];

int main() {
  int N, M;
  cin >> N >> M;

  vector<Jewelry> gems;
  for (int i = 0; i < N; ++i) {
    ll v, w;
    cin >> v >> w;
    gems.push_back(Jewelry(v, w));
  }

  sort(gems.begin(), gems.end());
  memset(dp, 0, sizeof(dp));
  ll ans = 0;

  for (int i = 0; i < N; ++i) {
    Jewelry j = gems[i];

    for (int w = M; w >= 0; --w) {
      int nw = w + j.w;
      ll nv = dp[i][w] + j.v;
      dp[i + 1][w] = dp[i][w];

      if (M < nw) continue;

      if (dp[i + 1][nw] < nv) {
        dp[i + 1][nw] = nv;
        ans = max(ans, nv * j.v);
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0