結果

問題 No.288 貯金箱の仕事
ユーザー chocoruskchocorusk
提出日時 2018-12-20 15:02:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,576 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 97,944 KB
実行使用メモリ 22,980 KB
最終ジャッジ日時 2023-10-26 01:01:11
合計ジャッジ時間 21,754 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
22,980 KB
testcase_01 AC 16 ms
22,980 KB
testcase_02 AC 15 ms
22,980 KB
testcase_03 AC 15 ms
22,980 KB
testcase_04 AC 15 ms
22,980 KB
testcase_05 AC 15 ms
22,980 KB
testcase_06 AC 15 ms
22,980 KB
testcase_07 AC 21 ms
22,980 KB
testcase_08 AC 15 ms
22,980 KB
testcase_09 AC 19 ms
22,980 KB
testcase_10 AC 16 ms
22,980 KB
testcase_11 AC 16 ms
22,980 KB
testcase_12 AC 16 ms
22,980 KB
testcase_13 AC 16 ms
22,980 KB
testcase_14 AC 16 ms
22,980 KB
testcase_15 AC 18 ms
22,980 KB
testcase_16 AC 17 ms
22,980 KB
testcase_17 AC 15 ms
22,980 KB
testcase_18 AC 16 ms
22,980 KB
testcase_19 AC 15 ms
22,980 KB
testcase_20 AC 15 ms
22,980 KB
testcase_21 AC 15 ms
22,980 KB
testcase_22 AC 17 ms
22,980 KB
testcase_23 AC 1,558 ms
22,980 KB
testcase_24 AC 768 ms
22,980 KB
testcase_25 AC 15 ms
22,980 KB
testcase_26 AC 15 ms
22,980 KB
testcase_27 AC 15 ms
22,980 KB
testcase_28 AC 16 ms
22,980 KB
testcase_29 AC 16 ms
22,980 KB
testcase_30 AC 47 ms
22,980 KB
testcase_31 AC 46 ms
22,980 KB
testcase_32 AC 242 ms
22,980 KB
testcase_33 AC 242 ms
22,980 KB
testcase_34 AC 1,179 ms
22,980 KB
testcase_35 AC 1,186 ms
22,980 KB
testcase_36 AC 1,576 ms
22,980 KB
testcase_37 AC 163 ms
22,980 KB
testcase_38 AC 1,049 ms
22,980 KB
testcase_39 AC 196 ms
22,980 KB
testcase_40 AC 241 ms
22,980 KB
testcase_41 AC 1,354 ms
22,980 KB
testcase_42 AC 1,157 ms
22,980 KB
testcase_43 AC 302 ms
22,980 KB
testcase_44 AC 356 ms
22,980 KB
testcase_45 AC 1,037 ms
22,980 KB
testcase_46 AC 1,137 ms
22,980 KB
testcase_47 AC 952 ms
22,980 KB
testcase_48 AC 874 ms
22,980 KB
testcase_49 AC 116 ms
22,980 KB
testcase_50 AC 590 ms
22,980 KB
testcase_51 AC 128 ms
22,980 KB
testcase_52 AC 293 ms
22,980 KB
testcase_53 AC 972 ms
22,980 KB
testcase_54 AC 527 ms
22,980 KB
testcase_55 AC 15 ms
22,980 KB
権限があれば一括ダウンロードができます

ソースコード

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