結果

問題 No.527 ナップサック容量問題
ユーザー kyo1kyo1
提出日時 2020-09-10 00:30:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 898 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 205,132 KB
実行使用メモリ 42,848 KB
最終ジャッジ日時 2023-08-22 15:49:42
合計ジャッジ時間 4,626 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 10 ms
9,892 KB
testcase_06 AC 36 ms
32,560 KB
testcase_07 AC 24 ms
22,000 KB
testcase_08 AC 8 ms
7,972 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 38 ms
32,824 KB
testcase_11 AC 20 ms
18,068 KB
testcase_12 AC 11 ms
10,904 KB
testcase_13 AC 39 ms
34,400 KB
testcase_14 AC 6 ms
7,012 KB
testcase_15 AC 14 ms
13,328 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 10 ms
10,340 KB
testcase_18 AC 13 ms
11,968 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 8 ms
7,604 KB
testcase_21 AC 49 ms
42,848 KB
testcase_22 AC 24 ms
21,004 KB
testcase_23 AC 44 ms
39,996 KB
testcase_24 AC 3 ms
4,692 KB
testcase_25 AC 20 ms
18,348 KB
testcase_26 AC 18 ms
16,272 KB
testcase_27 AC 18 ms
16,444 KB
testcase_28 AC 14 ms
13,276 KB
testcase_29 AC 48 ms
42,564 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 16 ms
15,216 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 10 ms
9,796 KB
testcase_38 AC 15 ms
13,860 KB
testcase_39 AC 13 ms
12,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> V(N), W(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  int X;
  cin >> X;
  vector<vector<int>> dp(N + 1, vector<int>((N + 1) * *max_element(W.begin(), W.end()) + 1, 0));
  for (int i = 0; i < N; i++) {
    for (int w = 0; w < (int)dp[0].size(); w++) {
      dp[i + 1][w] = max(dp[i + 1][w], dp[i][w]);
      if (w + W[i] < (int)dp[0].size())
        dp[i + 1][w + W[i]] = max(dp[i + 1][w + W[i]], dp[i][w] + V[i]);
    }
  }
  vector<int> res;
  for (int w = 1; w < (int)dp[0].size(); w++) {
    if (dp[N][w] == X) res.emplace_back(w);
  }
  cout << res.front() << '\n';
  int tot = 0;
  for (int v : V) {
    tot += v;
  }
  if (tot == X) {
    cout << "inf" << '\n';
  } else {
    cout << res.back() << '\n';
  }
  return 0;
}
0