結果

問題 No.288 貯金箱の仕事
ユーザー しらっ亭
提出日時 2016-03-16 01:43:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 1,306 ms
コンパイル使用メモリ 159,340 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-01 07:05:56
合計ジャッジ時間 5,780 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FORR(x,arr) for(auto&& x:arr)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
typedef long long ll;
// -------------------------------------

int n;
ll m;
ll a[500];
ll k[500];

template<class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

void Main() {
  cin >> n >> m;
  ll ma=0;
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> k[i];

  ll s = -m;
  REP(i, n) {
    amax(ma, a[i]);
    s += a[i] * k[i];
  }

  if (s < 0) {
    puts("-1");
    return;
  }

  ll ma2=ma*ma;
  ll ans = (s - s % ma2) / ma;
  s %= ma2;

  int dp[ma2+1];
  memset(dp, 0x3f, sizeof(dp));

  dp[0] = 0;
  REP(i, n) {
    REP(j, ma2+1) {
      if (dp[j] < 1e9) {
        if (j+a[i] <= ma2) amin(dp[j+a[i]], dp[j]+1);
      }
    }
  }
  cout << (dp[s] > 1e9 ? -1 : (ans + dp[s])) << endl;
}
int main() {Main(); return 0; }
0