結果
| 問題 |
No.527 ナップサック容量問題
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-06-09 22:57:53 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,100 bytes |
| コンパイル時間 | 659 ms |
| コンパイル使用メモリ | 58,192 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-22 16:43:26 |
| 合計ジャッジ時間 | 1,831 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 WA * 23 |
ソースコード
#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;
}