結果

問題 No.288 貯金箱の仕事
ユーザー pekempey
提出日時 2015-10-10 00:10:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,308 bytes
コンパイル時間 1,434 ms
コンパイル使用メモリ 159,152 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-07-20 04:52:10
合計ジャッジ時間 7,161 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

const ll inf = 1e9;
const ll MX = 333333;
ll N, M;
ll A[500], K[500];
int dp[2][MX];

ll solve(ll D) {
	rep (i, 2) rep (j, MX) dp[i][j] = inf;
	dp[0][0] = 0;
	rep (i, N) {
		rep (j, MX) {
			dp[(i + 1) % 2][j] = dp[i % 2][j];
			if (j - A[i] >= 0) {
				chmin(dp[(i + 1) % 2][j], dp[(i + 1) % 2][j - A[i]] + 1);
			}
		}
	}
	if (D < MX) {
		if (dp[N % 2][D] == inf) return -1;
		return dp[N % 2][D];
	} else {
		ll mx = *max_element(A, A + N);
		ll num = (D - MX + mx) / mx;
		ll res = dp[N % 2][D - num * mx] + num;
		return res;
	}
}

int main() {
	cin >> N >> M;
	rep (i, N) cin >> A[i];
	rep (i, N) cin >> K[i];
	ll mx = 0;
	rep (i, N) mx += A[i] * K[i];
	ll D = mx - M;
	if (D < 0) {
		cout << -1 << endl;	
	} else {
		ll ans = solve(D);
		cout << ans << endl;
	}
	return 0;
}
0