結果

問題 No.2869 yuusaan's Knapsacks
ユーザー Rubikun
提出日時 2024-08-30 21:43:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 875 ms / 4,500 ms
コード長 2,477 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 201,752 KB
最終ジャッジ日時 2025-02-24 02:57:55
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=15<<26;

ll dp[17][1<<16];
ll suma[1<<16],sumb[1<<16];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,M;cin>>N>>M;
    vl E(N);
    for(int i=0;i<N;i++) cin>>E[i];
    reverse(all(E));
    vll T(M);
    for(int i=0;i<M;i++){
        ll a,b;cin>>a>>b;
        T[i]=mp(b,a);
    }
    
    for(int i=0;i<=N;i++){
        for(int j=0;j<(1<<16);j++){
            dp[i][j]=-INF;
        }
    }
    
    for(int S=0;S<(1<<M);S++){
        ll aa=0,bb=0;
        for(int k=0;k<M;k++){
            if(S&(1<<k)){
                aa+=T[k].fi;
                bb+=T[k].se;
            }
        }
        suma[S]=aa;
        sumb[S]=bb;
    }
    dp[0][0]=0;
    
    for(int i=0;i<N;i++){
        for(int j=0;j<(1<<M);j++){
            if(dp[i][j]<0) continue;
            int rem=(1<<M)-1-j;
            for(int S=rem;;S=(S-1)&rem){
                if(suma[S]<=E[i]){
                    chmax(dp[i+1][j|S],dp[i][j]+sumb[S]);
                }
                if(S==0) break;
            }
        }
    }
    
    pair<ll,ll> ma=mp(-1,-1);
    for(int j=0;j<(1<<M);j++){
        chmax(ma,mp(dp[N][j],(ll)(j)));
    }
    
    cout<<ma.fi<<"\n";
    ll j=ma.se;
    
    for(int i=N-1;i>=0;i--){
        ll nex=-1;
        for(ll p=0;p<(1<<M);p++){
            if((p&j)==p){
                if(suma[j-p]<=E[i]){
                    nex=p;
                    break;
                }
            }
        }
        ll sa=j-nex;
        cout<<__builtin_popcount(sa);
        for(int x=0;x<M;x++){
            if(sa&(1<<x)) cout<<" "<<x+1;
        }
        cout<<"\n";
        j=nex;
    }
}

0