結果

問題 No.2869 yuusaan's Knapsacks
ユーザー rniyarniya
提出日時 2024-08-30 22:36:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 713 ms / 4,500 ms
コード長 2,712 bytes
コンパイル時間 3,183 ms
コンパイル使用メモリ 257,412 KB
実行使用メモリ 22,320 KB
最終ジャッジ日時 2024-08-30 22:37:03
合計ジャッジ時間 10,010 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    for (std::string_view sep = ""; const auto& e : v) {
        os << std::exchange(sep, " ") << e;
    }
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) {
    return y < x and (x = std::forward<U>(y), true);
}

template <class T, class U = T> bool chmax(T& x, U&& y) {
    return x < y and (x = std::forward<U>(y), true);
}

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

using ll = long long;

using namespace std;

const ll INF = 1e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    vector<int> e(N), v(M), w(M);
    for (int i = 0; i < N; i++) cin >> e[i];
    for (int i = 0; i < M; i++) cin >> v[i] >> w[i];

    vector<ll> V(1 << M, 0), W(1 << M, 0);
    for (int S = 0; S < 1 << M; S++) {
        for (int i = 0; i < M; i++) {
            if (S >> i & 1) {
                V[S] += v[i];
                W[S] += w[i];
            }
        }
    }
    vector dp(N + 1, vector<ll>(1 << M, -INF)), pre(N + 1, vector<ll>(1 << M, -1));
    dp[0][0] = 0;

    for (int i = 0; i < N; i++) {
        for (int S = 0; S < 1 << M; S++) {
            ll& val = dp[i][S];
            if (val == -INF) continue;
            int rest = (1 << M) - 1 - S;
            for (int T = rest;; T = (T - 1) & rest) {
                if (W[T] > e[i]) continue;
                if (chmax(dp[i + 1][S | T], val + V[T])) pre[i + 1][S | T] = T;
                if (T == 0) break;
            }
        }
    }

    ll maxi = -INF, argmax = -1;
    for (int S = 0; S < 1 << M; S++) {
        if (chmax(maxi, dp[N][S])) {
            argmax = S;
        }
    }
    vector<vector<int>> res(N);
    for (int i = N - 1; i >= 0; i--) {
        int T = pre[i + 1][argmax];
        for (int j = 0; j < M; j++) {
            if (T >> j & 1) {
                res[i].emplace_back(j);
            }
        }
        argmax ^= T;
    }

    cout << maxi << "\n";
    for (int i = 0; i < N; i++) {
        cout << res[i].size();
        for (int& x : res[i]) cout << " " << x + 1;
        cout << "\n";
    }
    return 0;
}
0