結果

問題 No.1858 Gorgeous Knapsack
ユーザー simansiman
提出日時 2022-02-27 20:15:15
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,050 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 142,440 KB
実行使用メモリ 199,808 KB
最終ジャッジ日時 2024-07-05 19:35:25
合計ジャッジ時間 9,396 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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