結果

問題 No.288 貯金箱の仕事
ユーザー kyuna
提出日時 2019-08-09 01:46:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 963 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 74,400 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 21:18:16
合計ジャッジ時間 3,728 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    int n; ll m; cin >> n >> m;
    vector<int> a(n);
    for (int &ai: a) cin >> ai;
    vector<ll> k(n);
    for (ll &ki: k) cin >> ki;
    ll tot = inner_product(a.begin(), a.end(), k.begin(), 0LL);
    if (tot < m) {
        cout << -1 << endl;
        return 0;
    }
    const int ma = a.back() * a.back();
    vector<ll> dp(ma + 1, INF);
    dp[0] = 0;
    for (int ai: a) for (int j = ai; j <= ma; j++) chmin(dp[j], dp[j - ai] + 1);
    ll otsuri = tot - m;
    ll c = max(0LL, (otsuri - ma + a.back()) / a.back());
    if (dp[otsuri - c * a.back()] == INF) {
        cout << -1 << endl;
    } else {
        cout << c + dp[otsuri - c * a.back()] << endl;
    }
    return 0;
}
0