結果

問題 No.15 カタログショッピング
ユーザー uafr_csuafr_cs
提出日時 2015-10-12 01:09:34
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,226 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 76,384 KB
実行使用メモリ 60,236 KB
最終ジャッジ日時 2023-09-28 12:28:37
合計ジャッジ時間 19,620 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
57,572 KB
testcase_01 AC 129 ms
55,908 KB
testcase_02 AC 132 ms
55,608 KB
testcase_03 AC 169 ms
56,244 KB
testcase_04 AC 131 ms
55,868 KB
testcase_05 AC 923 ms
60,024 KB
testcase_06 AC 4,040 ms
58,804 KB
testcase_07 AC 4,826 ms
60,236 KB
testcase_08 TLE -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

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 dfs(final long S, final int N, final int deep, int[] items, int[] maximum, LinkedList<Integer> stack){
		if(S == 0){
			//
			boolean first = true;
			
			for(final int item : stack){
				if(first){
					first = false;
				}else{
					System.out.print(" ");
				}
				
				System.out.print(item + 1);
			}
			System.out.println();
			
			return;
		}else if(deep >= N){
			return;
		}else if(maximum[deep] < S){
			return;
		}
		
		if(S >= items[deep]){
			stack.addLast(deep);
			dfs(S - items[deep], N, deep + 1, items, maximum, stack);
			stack.removeLast();
		}
		dfs(S, N, deep + 1, items, maximum, stack);
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final long S = sc.nextLong();
		
		int[] items = new int[N];
		for(int i = 0; i < N; i++){
			items[i] = sc.nextInt();
		}
		
		int[] maximum = new int[N];
		for(int i = N - 1; i >= 0; i--){
			maximum[i] = items[i] + (i == N - 1 ? 0 : maximum[i + 1]);
		}
		
		dfs(S, N, 0, items, maximum, new LinkedList<Integer>());
	}

}
0