結果

問題 No.527 ナップサック容量問題
ユーザー Hachimori
提出日時 2017-06-09 23:12:13
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,100 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 58,196 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-22 18:09:51
合計ジャッジ時間 1,972 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<numeric>
#include<cstring>
using namespace std;
const int N_BURDEN = 105;
const int SUM_VALUE = 100005;


int N;
int value[N_BURDEN], weight[N_BURDEN];
int V;

void read() {
    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> value[i] >> weight[i];
    }
    cin >> V;
}


void work() {
    int maxV[SUM_VALUE];
    memset(maxV, -1, sizeof(maxV));
    maxV[0] = 0;

    for (int i = 0; i < N; ++i) {
        for (int j = SUM_VALUE - 1; j >= 0; --j) {
            if (maxV[j] != -1) {
                maxV[j + weight[i]] = max(maxV[j + weight[i]], maxV[j] + value[i]);
            }
        }
    }
    
    int idx;
    for (idx = 0;; ++idx) {
        if (maxV[idx] == V) {
            if (idx == 0) ++idx;
            cout << idx << endl;
            break;
        }
    }
    
    if (maxV[accumulate(weight, weight + N, 0)] == V) {
        cout << "inf" << endl;
    } else {
        while (maxV[idx] == -1 || maxV[idx] <= V) ++idx;
        cout << max(1, idx - 1) << endl;
    }
}


int main() {
    read();
    work();
    return 0;
}
0