結果

問題 No.286 Modulo Discount Store
ユーザー htensai
提出日時 2020-02-07 09:24:23
言語 Java
(openjdk 23)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 2,055 ms
コンパイル使用メモリ 77,332 KB
実行使用メモリ 51,188 KB
最終ジャッジ日時 2024-09-25 07:13:59
合計ジャッジ時間 5,889 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static int n;
    static int[] prices;
    static int[] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        prices = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = Integer.parseInt(br.readLine());
        }
        dp = new int[1 << n];
        System.out.println(dfw((1 << n) - 1));
   }
   
   static int dfw(int key) {
       if (key == 0) {
           return 0;
       }
       if (dp[key] != 0) {
           return dp[key];
       }
       int sum = 0;
       for (int i = 0; i < n; i++) {
           if (((1 << i) & key) != 0) {
               sum += prices[i];
           }
       }
       int min = Integer.MAX_VALUE;
       for (int i = 0; i < n; i++) {
           if (((1 << i) & key) == 0) {
               continue;
           }
           min = Math.min(min, dfw(key ^ (1 << i)) + prices[i] - Math.min(prices[i], (sum - prices[i]) % 1000));
       }
       dp[key] = min;
       return min;
   }
}
0