結果

問題 No.288 貯金箱の仕事
ユーザー Yang33Yang33
提出日時 2018-09-28 00:17:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 3,217 bytes
コンパイル時間 2,160 ms
コンパイル使用メモリ 167,668 KB
実行使用メモリ 5,032 KB
最終ジャッジ日時 2023-08-02 09:51:40
合計ジャッジ時間 5,855 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,848 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 158 ms
4,796 KB
testcase_24 AC 81 ms
4,880 KB
testcase_25 AC 3 ms
4,788 KB
testcase_26 AC 4 ms
4,880 KB
testcase_27 AC 3 ms
4,788 KB
testcase_28 AC 4 ms
4,816 KB
testcase_29 AC 3 ms
4,792 KB
testcase_30 AC 43 ms
4,828 KB
testcase_31 AC 43 ms
4,892 KB
testcase_32 AC 82 ms
4,848 KB
testcase_33 AC 81 ms
4,836 KB
testcase_34 AC 120 ms
4,876 KB
testcase_35 AC 120 ms
4,816 KB
testcase_36 AC 159 ms
4,816 KB
testcase_37 AC 47 ms
4,820 KB
testcase_38 AC 110 ms
4,864 KB
testcase_39 AC 55 ms
4,824 KB
testcase_40 AC 56 ms
4,920 KB
testcase_41 AC 152 ms
4,876 KB
testcase_42 AC 132 ms
4,932 KB
testcase_43 AC 93 ms
4,876 KB
testcase_44 AC 28 ms
4,376 KB
testcase_45 AC 86 ms
4,380 KB
testcase_46 AC 134 ms
4,924 KB
testcase_47 AC 67 ms
4,560 KB
testcase_48 AC 92 ms
4,936 KB
testcase_49 AC 40 ms
4,380 KB
testcase_50 AC 58 ms
4,560 KB
testcase_51 AC 40 ms
4,412 KB
testcase_52 AC 74 ms
4,880 KB
testcase_53 AC 95 ms
5,032 KB
testcase_54 AC 55 ms
4,560 KB
testcase_55 AC 1 ms
4,380 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()) / a.back());
		Pay -= cnt * a.back();
		ans += cnt;
		// ここで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