結果

問題 No.286 Modulo Discount Store
ユーザー uafr_csuafr_cs
提出日時 2015-10-11 23:55:28
言語 Java19
(openjdk 21)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 1,449 bytes
コンパイル時間 2,549 ms
コンパイル使用メモリ 75,616 KB
実行使用メモリ 58,396 KB
最終ジャッジ日時 2023-09-28 12:25:28
合計ジャッジ時間 10,277 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,668 KB
testcase_01 AC 127 ms
55,712 KB
testcase_02 AC 153 ms
55,804 KB
testcase_03 AC 125 ms
56,196 KB
testcase_04 AC 127 ms
55,632 KB
testcase_05 AC 125 ms
55,856 KB
testcase_06 AC 166 ms
57,996 KB
testcase_07 AC 125 ms
55,912 KB
testcase_08 AC 124 ms
55,944 KB
testcase_09 AC 125 ms
55,528 KB
testcase_10 AC 146 ms
55,788 KB
testcase_11 AC 124 ms
55,812 KB
testcase_12 AC 125 ms
55,928 KB
testcase_13 AC 154 ms
55,756 KB
testcase_14 AC 123 ms
55,540 KB
testcase_15 AC 127 ms
55,912 KB
testcase_16 AC 126 ms
55,788 KB
testcase_17 AC 191 ms
58,396 KB
testcase_18 AC 146 ms
56,000 KB
testcase_19 AC 124 ms
55,704 KB
testcase_20 AC 130 ms
56,128 KB
testcase_21 AC 128 ms
56,040 KB
testcase_22 AC 124 ms
55,924 KB
testcase_23 AC 157 ms
55,836 KB
testcase_24 AC 126 ms
55,700 KB
testcase_25 AC 136 ms
55,732 KB
testcase_26 AC 127 ms
55,976 KB
testcase_27 AC 139 ms
55,516 KB
testcase_28 AC 168 ms
57,940 KB
testcase_29 AC 129 ms
54,144 KB
testcase_30 AC 125 ms
56,060 KB
testcase_31 AC 127 ms
55,980 KB
testcase_32 AC 126 ms
55,736 KB
testcase_33 AC 124 ms
56,052 KB
testcase_34 AC 127 ms
55,972 KB
testcase_35 AC 127 ms
55,924 KB
testcase_36 AC 124 ms
55,488 KB
testcase_37 AC 140 ms
56,008 KB
testcase_38 AC 125 ms
55,756 KB
testcase_39 AC 169 ms
57,792 KB
testcase_40 AC 125 ms
55,560 KB
testcase_41 AC 126 ms
55,696 KB
testcase_42 AC 124 ms
55,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int MOD = 1000;
		
		int[] items = new int[N];
		for(int i = 0; i < N; i++){
			items[i] = sc.nextInt();
		}
		
		final int bit_size = 1 << N;
		int[] mods = new int[bit_size];
		for(int bit = 0; bit < bit_size; bit++){
			for(int not_buy = 0; not_buy < N; not_buy++){
				if((bit & (1 << not_buy)) == 0){
					mods[bit | (1 << not_buy)] = (mods[bit] + items[not_buy]) % MOD;
				}
			}
		}
		
		int[][] DP = new int[bit_size][N];
		for(int i = 0; i < bit_size; i++){
			for(int j = 0; j < N; j++){
				DP[i][j] = Integer.MAX_VALUE;
			}
		}
		
		for(int i = 0; i < N; i++){
			DP[1 << i][i] = items[i];
		}
		
		for(int bit = 1; bit < bit_size; bit++){
			for(int last_buy = 0; last_buy < N; last_buy++){
				if(DP[bit][last_buy] == Integer.MAX_VALUE){
					continue;
				}
				
				for(int new_buy = 0; new_buy < N; new_buy++){
					if((bit & (1 << new_buy)) != 0){
						continue;
					}
					
					final int next_bit = bit | (1 << new_buy);
					DP[next_bit][new_buy] = Math.min(DP[next_bit][new_buy], DP[bit][last_buy] + items[new_buy] - Math.min(items[new_buy], mods[bit]));
				}
			}
		}
		
		System.out.println(Arrays.stream(DP[bit_size - 1]).min().getAsInt());
	}

}
0