結果

問題 No.288 貯金箱の仕事
ユーザー chocorusk
提出日時 2018-12-20 15:17:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 94,948 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-25 09:00:47
合計ジャッジ時間 3,988 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll INF=1e18+1;
int main()
{
	int n; ll m;
	cin>>n>>m;
	ll a[500];
	for(int i=0; i<n; i++) cin>>a[i];
	ll s=0;
	for(int i=0; i<n; i++){
		ll k; cin>>k;
		s+=(a[i]*k);
	}
	if(s<m){
		cout<<-1<<endl;
		return 0;
	}
	s-=m;
	ll dp[250001];
	fill(dp, dp+250001, INF);
	dp[0]=0;
	for(int i=0; i<n-1; i++){
		for(int j=0; j<=250000-a[i]; j++){
			dp[j+a[i]]=min(dp[j]+1, dp[j+a[i]]);
		}
	}
	ll ans=INF;
	for(ll i=s%a[n-1]; i<=min(250000ll, s); i+=a[n-1]){
		if(dp[i]==INF) continue;
		ans=min(ans, dp[i]+(s-i)/a[n-1]);
	}
	if(ans<INF) cout<<ans<<endl;
	else cout<<-1<<endl;
	return 0;
}
0