結果

問題 No.286 Modulo Discount Store
ユーザー imulan
提出日時 2016-07-13 18:11:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 936 bytes
コンパイル時間 1,617 ms
コンパイル使用メモリ 166,676 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-14 15:43:21
合計ジャッジ時間 2,990 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int n;
int m[15];

int dp[1<<15];
int dfs(int mask)
{
    if(dp[mask]>=0) return dp[mask];

    //全ての商品購入が終わった
    if(mask==(1<<n)-1) return 0;

    //値引き額
    int discount=0;
    rep(i,n)
    {
        if(mask>>i&1) (discount+=m[i])%=1000;
    }

    int ret=12345678;
    //次に買う商品i
    rep(i,n)
    {
        if(mask>>i&1) continue;
        int add=max(0,m[i]-discount);
        ret=min(ret,dfs(mask+(1<<i))+add);
    }

    return dp[mask]=ret;
}

int main()
{
    cin >>n;
    rep(i,n) cin >>m[i];

    memset(dp,-1,sizeof(dp));
    cout << dfs(0) << endl;
    return 0;
}
0