結果

問題 No.288 貯金箱の仕事
ユーザー rpy3cpp
提出日時 2016-03-10 23:14:30
言語 C++11
(gcc 4.8.5)
結果
AC  
実行時間 128 ms
コード長 1,444 Byte
コンパイル時間 224 ms
使用メモリ 2,484 KB
最終ジャッジ日時 2019-07-06 10:04:15

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00-sample-01.txt AC 6 ms
2,476 KB
00-sample-02.txt AC 3 ms
2,476 KB
00-sample-03.txt AC 5 ms
2,476 KB
01-small-01.txt AC 4 ms
2,476 KB
01-small-02.txt AC 2 ms
1,500 KB
01-small-03.txt AC 3 ms
1,500 KB
01-small-04.txt AC 2 ms
1,500 KB
01-small-05.txt AC 16 ms
2,480 KB
01-small-06.txt AC 8 ms
2,480 KB
01-small-07.txt AC 14 ms
2,484 KB
01-small-08.txt AC 15 ms
2,480 KB
01-small-09.txt AC 8 ms
2,480 KB
01-small-10.txt AC 14 ms
2,480 KB
01-small-11.txt AC 12 ms
2,484 KB
01-small-12.txt AC 13 ms
2,480 KB
01-small-13.txt AC 16 ms
2,480 KB
01-small-14.txt AC 14 ms
2,480 KB
01-small-15.txt AC 12 ms
2,480 KB
01-small-16.txt AC 9 ms
2,484 KB
01-small-17.txt AC 11 ms
2,476 KB
01-small-18.txt AC 8 ms
2,476 KB
01-small-19.txt AC 9 ms
2,484 KB
01-small-20.txt AC 13 ms
2,484 KB
02-large-01.txt AC 127 ms
2,480 KB
02-large-02.txt AC 100 ms
2,480 KB
02-large-03.txt AC 5 ms
2,480 KB
02-large-04.txt AC 4 ms
2,480 KB
02-large-05.txt AC 5 ms
2,476 KB
02-large-06.txt AC 4 ms
2,480 KB
02-large-07.txt AC 4 ms
2,480 KB
02-large-08.txt AC 35 ms
2,480 KB
02-large-09.txt AC 36 ms
2,484 KB
02-large-10.txt AC 66 ms
2,480 KB
02-large-11.txt AC 66 ms
2,480 KB
02-large-12.txt AC 97 ms
2,480 KB
02-large-13.txt AC 97 ms
2,480 KB
02-large-14.txt AC 128 ms
2,480 KB
02-large-15.txt AC 39 ms
2,484 KB
02-large-16.txt AC 89 ms
2,484 KB
02-large-17.txt AC 45 ms
2,480 KB
02-large-18.txt AC 48 ms
2,480 KB
02-large-19.txt AC 122 ms
2,484 KB
02-large-20.txt AC 107 ms
2,480 KB
02-large-21.txt AC 76 ms
2,480 KB
02-large-22.txt AC 54 ms
2,480 KB
02-large-23.txt AC 98 ms
2,480 KB
02-large-24.txt AC 108 ms
2,484 KB
02-large-25.txt AC 71 ms
2,484 KB
02-large-26.txt AC 79 ms
2,484 KB
02-large-27.txt AC 49 ms
2,476 KB
02-large-28.txt AC 63 ms
2,484 KB
02-large-29.txt AC 47 ms
2,480 KB
02-large-30.txt AC 65 ms
2,480 KB
02-large-31.txt AC 83 ms
2,484 KB
02-large-32.txt AC 58 ms
2,480 KB
challenge01.txt AC 5 ms
2,476 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <iostream>
using namespace std;

constexpr int N2 = 250010;
int dp[N2] = {0};
int A[501] = {0};
long long K[501] = {0};


int main()
{
    int N;
    long long M;
    cin >> N >> M;
    for (int i = 0; i != N; ++i) cin >> A[i];
    for (int i = 0; i != N; ++i) cin >> K[i];
    long long X = 0;
    for (int i = 0; i != N; ++i) X += A[i] * K[i];
    X -= M;
    if (X < 0){
        cout << - 1 << endl;
        return 0;
    }else if (X == 0){
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i != N; ++i){
        int a = A[i];
        dp[a] = 1;
        int limit = N2 - a;
        for (int j = 1; j != limit; ++j){
            if (dp[j] and (dp[j] + 1 < dp[j + a] or not dp[j + a])){
                dp[j + a] = dp[j] + 1;
            }
        }
    }
    if (X < N2){
        if (dp[X] == 0){
            cout << - 1 << endl;
        }else{
            cout << dp[X] << endl;
        }
        return 0;
    }
    
    // X = maxA * k + residue
    // 0 <= X - maxA * k < N2
    // maxA * k <= X < N2 + maxA * k
    // (X - N2)/maxA < k <= X / maxA
    int maxA = A[N - 1];
    long long min_coins = X + 1;
    for (long long k = (X - N2)/maxA + 1; k * maxA <= X; ++k){
        int coins = dp[X - k * maxA];
        if (coins and coins + k < min_coins) min_coins = coins + k;
    }
    if (min_coins == X + 1){
        cout << - 1 << endl;
    }else{
        cout << min_coins << endl;
    }
    return 0;
}
0