結果

問題 No.288 貯金箱の仕事
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-02-12 22:22:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 1,700 bytes
コンパイル時間 2,524 ms
コンパイル使用メモリ 78,396 KB
実行使用メモリ 60,816 KB
最終ジャッジ日時 2023-09-06 00:12:59
合計ジャッジ時間 14,556 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,760 KB
testcase_01 AC 116 ms
55,784 KB
testcase_02 AC 116 ms
56,084 KB
testcase_03 AC 116 ms
58,092 KB
testcase_04 AC 113 ms
55,928 KB
testcase_05 AC 119 ms
56,304 KB
testcase_06 AC 118 ms
53,756 KB
testcase_07 AC 126 ms
55,680 KB
testcase_08 AC 122 ms
56,080 KB
testcase_09 AC 125 ms
55,956 KB
testcase_10 AC 126 ms
55,984 KB
testcase_11 AC 119 ms
55,748 KB
testcase_12 AC 124 ms
55,724 KB
testcase_13 AC 121 ms
54,584 KB
testcase_14 AC 120 ms
56,100 KB
testcase_15 AC 122 ms
55,868 KB
testcase_16 AC 122 ms
56,140 KB
testcase_17 AC 123 ms
56,260 KB
testcase_18 AC 121 ms
56,172 KB
testcase_19 AC 127 ms
56,312 KB
testcase_20 AC 112 ms
55,768 KB
testcase_21 AC 122 ms
56,164 KB
testcase_22 AC 117 ms
56,040 KB
testcase_23 AC 182 ms
57,260 KB
testcase_24 AC 168 ms
56,684 KB
testcase_25 AC 146 ms
60,816 KB
testcase_26 AC 140 ms
58,700 KB
testcase_27 AC 145 ms
58,612 KB
testcase_28 AC 147 ms
58,588 KB
testcase_29 AC 142 ms
59,080 KB
testcase_30 AC 353 ms
59,016 KB
testcase_31 AC 350 ms
58,640 KB
testcase_32 AC 309 ms
58,424 KB
testcase_33 AC 317 ms
58,620 KB
testcase_34 AC 326 ms
58,772 KB
testcase_35 AC 371 ms
58,868 KB
testcase_36 AC 194 ms
57,448 KB
testcase_37 AC 168 ms
56,852 KB
testcase_38 AC 192 ms
56,756 KB
testcase_39 AC 172 ms
56,832 KB
testcase_40 AC 160 ms
55,984 KB
testcase_41 AC 198 ms
57,720 KB
testcase_42 AC 188 ms
56,780 KB
testcase_43 AC 179 ms
56,352 KB
testcase_44 AC 165 ms
56,276 KB
testcase_45 AC 189 ms
57,136 KB
testcase_46 AC 209 ms
57,420 KB
testcase_47 AC 184 ms
56,892 KB
testcase_48 AC 192 ms
56,672 KB
testcase_49 AC 150 ms
56,720 KB
testcase_50 AC 189 ms
56,660 KB
testcase_51 AC 173 ms
56,240 KB
testcase_52 AC 185 ms
56,556 KB
testcase_53 AC 191 ms
57,240 KB
testcase_54 AC 207 ms
57,016 KB
testcase_55 AC 114 ms
55,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) { new Main().run(); }
	Scanner in = new Scanner(System.in);
	void _out(Object...os) { System.out.println(deepToString(os)); }
	void _err(Object...os) { System.err.println(deepToString(os)); }
	
	void run() {
		long INF = 1_000_000_000_000_000_000L + 1;
		for (; in.hasNext(); ) {
			int n = in.nextInt();
			long m = in.nextLong();
			int[] as = new int[n];
			for (int i = 0; i < n; ++i) {
				as[i] = in.nextInt();
			}
			long[] ks = new long[n];
			for (int i = 0; i < n; ++i) {
				ks[i] = in.nextLong();
			}
			int lim = as[n - 1] << 1;
			long[] dp;
			for (; ; lim <<= 1) {
				dp = new long[lim + 1];
				for (int x = 0; x <= lim; ++x) {
					dp[x] = INF;
				}
				dp[0] = 0;
				for (int i = 0; i < n; ++i) {
					for (int x = as[i]; x <= lim; ++x) {
						if (dp[x] > dp[x - as[i]] + 1) {
							dp[x] = dp[x - as[i]] + 1;
						}
					}
				}
				boolean ok = true;
				for (int x = lim - as[n - 1]; x <= lim; ++x) {
					ok &= (dp[x] >= INF || dp[x] == dp[x - as[n - 1]] + 1);
				}
				if (ok) {
					break;
				}
			}
			_err(lim);
			long target = -m;
			for (int i = 0; i < n; ++i) {
				target += as[i] * ks[i];
			}
			long ans;
			if (target < 0) {
				ans = INF;
			} else if (target <= lim) {
				ans = dp[(int)target];
			} else {
				long greed = ((target - lim) + as[n - 1] - 1) / as[n - 1];
				ans = greed + dp[(int)(target - as[n - 1] * greed)];
			}
			System.out.println((ans >= INF) ? -1 : ans);
		}
	}
}
0