結果

問題 No.2869 yuusaan's Knapsacks
ユーザー 👑 emthrmemthrm
提出日時 2024-08-30 22:58:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 4,500 ms
コード長 2,183 bytes
コンパイル時間 3,309 ms
コンパイル使用メモリ 258,548 KB
実行使用メモリ 72,176 KB
最終ジャッジ日時 2024-08-30 22:58:26
合計ジャッジ時間 6,090 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 36 ms
24,992 KB
testcase_04 AC 36 ms
25,000 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 148 ms
72,044 KB
testcase_07 AC 45 ms
24,996 KB
testcase_08 AC 9 ms
7,936 KB
testcase_09 AC 9 ms
7,936 KB
testcase_10 AC 65 ms
37,864 KB
testcase_11 AC 23 ms
16,420 KB
testcase_12 AC 121 ms
59,176 KB
testcase_13 AC 23 ms
16,420 KB
testcase_14 AC 56 ms
29,288 KB
testcase_15 AC 43 ms
29,284 KB
testcase_16 AC 54 ms
33,576 KB
testcase_17 AC 158 ms
72,172 KB
testcase_18 AC 115 ms
55,024 KB
testcase_19 AC 84 ms
42,148 KB
testcase_20 AC 8 ms
7,936 KB
testcase_21 AC 88 ms
46,440 KB
testcase_22 AC 96 ms
46,312 KB
testcase_23 AC 28 ms
20,704 KB
testcase_24 AC 130 ms
59,304 KB
testcase_25 AC 15 ms
12,136 KB
testcase_26 AC 106 ms
46,444 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 110 ms
72,172 KB
testcase_29 AC 146 ms
72,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n, m; cin >> n >> m;
  vector<int> e(n);
  for (int& e_i : e) cin >> e_i;
  vector<int> v(m), w(m); REP(i, m) cin >> v[i] >> w[i];
  vector dp(n * m + 1, vector(1 << m, INF));
  vector prv(n * m + 1, vector(1 << m, false));
  dp[0][0] = 0;
  REP(i, n) REP(j, m) {
    REP(k, 1 << m) {
      if (dp[i * m + j][k] == INF) continue;
      if (chmin(dp[i * m + j + 1][k], dp[i * m + j][k])) prv[i * m + j + 1][k] = false;
      if (k >> j & 1) continue;
      const int weight = dp[i * m + j][k] + w[j];
      if (weight <= e[i] && chmin(dp[i * m + j + 1][k | (1 << j)], weight)) prv[i * m + j + 1][k | (1 << j)] = true;
    }
    if (j + 1 == m) {
      REP(k, 1 << m) {
        if (dp[(i + 1) * m][k] != INF) dp[(i + 1) * m][k] = 0;
      }
    }
  }
  int ans_v = 0, argmax_k = 0;
  FOR(k, 1, 1 << m) {
    if (dp[n * m][k] == INF) continue;
    int value = 0;
    REP(l, m) {
      if (k >> l & 1) value += v[l];
    }
    if (chmax(ans_v, value)) argmax_k = k;
  }
  vector<vector<int>> ans(n);
  for (int i = n - 1; i >= 0; --i) for (int j = m - 1; j >= 0; --j) {
    if (prv[i * m + j + 1][argmax_k]) {
      ans[i].emplace_back(j);
      argmax_k ^= 1 << j;
    }
  }
  cout << ans_v << '\n';
  REP(i, n) {
    cout << ans[i].size();
    for (const int e : ans[i]) cout << ' ' << e + 1;
    cout << '\n';
  }
  return 0;
}
0