結果

問題 No.286 Modulo Discount Store
ユーザー bekasa001
提出日時 2015-10-09 22:47:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 38,784 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 04:23:27
合計ジャッジ時間 1,256 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

const int MOD = 1000;
const int INF = 1000000007;

bool inmin(int &a, const int b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}

int dfs(const int *A, int B, int *memo, int P, const int N, int C) {
  if(memo[B] >= 0) {
    return memo[B];
  }
  if(P == N) {
    return memo[B] = 0;
  }
  int ans = INF;
  for(int i = 0; i < N; i++) {
    if(B & (1 << i)) {
      int cost = max(0, A[i] - (C % MOD));
      inmin(ans, dfs(A, B - (1 << i), memo, P + 1, N, C + A[i]) + cost);
    }
  }
  return memo[B] = ans;
}

int main() {
  int N;
  if(scanf("%d", &N) == EOF) {
    return 0;
  }
  int M[N];
  for(int i = 0; i < N; i++) {
    if(scanf("%d", M + i) == EOF) {
      return 0;
    }
  }
  int memo[1 << N];
  fill(memo, memo + (1 << N), -1);
  printf("%d\n", dfs(M, (1 << N) - 1, memo, 0, N, 0));
  return 0;
}
0