結果

問題 No.247 線形計画問題もどき
ユーザー not_522not_522
提出日時 2015-08-19 01:13:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 146,228 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 17:31:41
合計ジャッジ時間 2,428 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 17 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 17 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 17 ms
4,376 KB
testcase_25 AC 12 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename Weight, typename Value, bool strict = false> Value unboundedKnapsack(Weight maxWeight, const vector<Weight>& weight, const vector<Value>& value) {
  constexpr Value IMP = numeric_limits<Value>::min();
  vector<Value> dp(maxWeight + Weight(1));
  if (strict) fill(dp.begin() + 1, dp.end(), IMP);
  for (size_t i = 0; i < weight.size(); ++i) {
    for (int w = 0; w <= maxWeight; ++w) {
      if (strict && dp[w] == IMP) continue;
      Weight ww = Weight(w) + weight[i];
      Value vv = dp[w] + value[i];
      if (ww <= maxWeight && dp[ww] < vv) dp[ww] = vv;
    }
  }
  return dp[maxWeight];
}

template<typename Weight, typename Value = long long> vector<Value> knapsackCount(Weight maxWeight, const vector<Weight>& weight) {
  vector<Value> dp(maxWeight + Weight(1));
  dp[0] = 1;
  for (auto& w : weight) {
    for (int i = 0; i <= maxWeight; ++i) {
      Weight ww = Weight(i) + w;
      if (ww <= maxWeight) dp[ww] += dp[i];
    }
  }
  return dp;
}

template<typename Weight> vector<bool> unboundedKnapsackFill(Weight maxWeight, const vector<Weight>& weight) {
  vector<bool> dp(maxWeight + Weight(1));
  dp[0] = true;
  for (auto& w : weight) {
    for (int i = 0; i <= maxWeight; ++i) {
      Weight ww = Weight(i) + w;
      if (ww <= maxWeight && dp[i]) dp[ww] = true;
    }
  }
  return dp;
}

int main() {
  int c, n;
  cin >> c >> n;
  vector<int> w(n), v(n, -1);
  for (int& i : w) cin >> i;
  auto res = unboundedKnapsack<int, int, true>(c, w, v);
  cout << (res != numeric_limits<int>::min() ? -res : -1) << endl;
}
0