結果

問題 No.288 貯金箱の仕事
ユーザー Yang33Yang33
提出日時 2018-09-28 00:11:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,230 bytes
コンパイル時間 1,596 ms
コンパイル使用メモリ 167,000 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 09:15:54
合計ジャッジ時間 7,841 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 RE -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 2 ms
5,376 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 AC 86 ms
5,376 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 AC 35 ms
5,376 KB
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/09/27  Problem: yukicoder 288  / Link: http://yukicoder.me/problems/no/288  ----- */
/* ------問題------

貯金箱さんは A 国に住んでいます.この国では通貨の単位は円と呼ばれ,A1 円玉,A2 円玉,…,AN 円玉の,N 種類の硬貨が使われています.
貯金箱さんは硬貨を貯めに貯めて,どの硬貨も 10100 枚持っています.あるとき,ゆきうさぎさんが現れ,M 円の貯金をしたいと言いました.ゆきうさぎさんは,今,A1 円玉を K1 枚,A2 円玉を K2 枚,…,AN 円玉を KN 枚持っています.貯金箱さんは,貯金後にゆきうさぎさんが持っていることになる硬貨の枚数を最小にしてあげようと思いました.このために,ゆきうさぎさんに一旦多めに貯金してもらってからお釣りを返すという方法をとることにしました.正確には,非負整数 x を選び,まずゆきうさぎさんが貯金箱さんに M+x 円を硬貨でちょうど支払ってから,貯金箱さんがゆきうさぎさんに x 円を硬貨でちょうど支払います.
貯金後にゆきうさぎさんの持っていることになる硬貨の枚数の最小値をプログラムを求めてください.ただし,ちょうど M 円を貯金することが不可能ならば,それを指摘してください.

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	LL N, M; cin >> N >> M;
	VL a(N);
	FOR(i, 0, N) {
		cin >> a[i];
	}
	VL k(N);
	FOR(i, 0, N) {
		cin >> k[i];
	}
	LL sum = 0;
	FOR(i, 0, N) {
		sum += a[i] * k[i];
	}
	if (sum < M) {
		ans = -1;
	}
	else {
		// 全額払うと、適当に最小化してくれる
		// そのdpをすればよい
		int upper = a.back()*a.back();
		LL Pay = sum - M;
		// Payを払うための最小枚数を求める
		LL cnt = max(0LL,(Pay - upper) / a.back());
		Pay -= cnt * a.back();
		ans += cnt;
		assert(Pay < upper);
		// ここでDP
		VL dp(upper + 1, LINF);
		dp[0] = 0;
		FOR(i, 0, N) {
			FOR(u, 0, upper + 1 - a[i]) {
				dp[u+a[i]] = min(dp[u+a[i]], dp[u]+1);
			}
		}
		if (dp[Pay] == LINF)ans = -1;
		else ans += dp[Pay];
	}

	cout << ans << "\n";

	return 0;
}
0