結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー risujirohrisujiroh
提出日時 2019-12-14 00:26:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 837 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 176,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 23:27:55
合計ジャッジ時間 4,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> p(n), d(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i] >> d[i];
  }
  vector<int> idx(n);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [&](int i, int j) { 
    return p[i] > p[j]; 
  });
  auto chmax = [](auto&& l, auto r) { l = max(l, r); };
  vector< vector<int> > dp(2, vector<int>(k + 1, -1e9));
  dp[0][0] = 0;
  for (int i : idx) {
    auto ndp = dp;
    for (int x = 0; x <= k; ++x) {
      if (x + p[i] <= k) {
        chmax(ndp[1][x + p[i]], dp[0][x] + d[i]);
      }
      chmax(ndp[0][x], dp[1][x] + d[i]);
    }
    swap(dp, ndp);
  }
  int res = 0;
  for (auto v : dp) {
    chmax(res, *max_element(begin(v), end(v)));
  }
  cout << res << '\n';
}
0