結果

問題 No.288 貯金箱の仕事
ユーザー chocoruskchocorusk
提出日時 2018-12-20 15:02:24
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 1,294 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 945 ms
コンパイル使用メモリ 97,336 KB
実行使用メモリ 23,040 KB
最終ジャッジ日時 2024-09-25 09:00:35
合計ジャッジ時間 19,397 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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>
#include <cassert>
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];
	ll sum=0;
	for(int i=0; i<n; i++){
		cin>>a[i];
		sum+=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;
	if(sum>50000){
	    ll dp[2500001];
	    fill(dp, dp+2500000, INF);
    	dp[0]=0;
	    for(int i=0; i<n-1; i++){
	    	for(int j=0; j<=2500000-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(2500000ll, 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;
	}
	ll sum1=sum;
	ll dp[2][300000];
	fill(dp[0], dp[0]+sum+1, INF);
	dp[0][0]=0;
	for(int t=0; t<60; t++){
		for(int i=0; i<n; i++){
			for(int j=sum1; j>=a[i]; j--){
				dp[t&1][j]=min(dp[t&1][j], dp[t&1][j-a[i]]+(1ll<<t));
			}
		}
		fill(dp[(t+1)&1], dp[(t+1)&1]+sum1/2+sum+1, INF);
		for(int j=(s&1); j<=sum1; j+=2){
			dp[(t+1)&1][j/2]=dp[t&1][j];
		}
		s>>=1;
		if(s==0){
			if(dp[(t+1)&1][0]<INF) cout<<dp[(t+1)&1][0]<<endl;
			else cout<<-1<<endl;
			return 0;
		}
		sum1=sum1/2+sum;
	}
	return 0;
}
0