結果

問題 No.527 ナップサック容量問題
ユーザー olpheolphe
提出日時 2017-06-09 22:26:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,345 bytes
コンパイル時間 1,907 ms
コンパイル使用メモリ 111,952 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 21:39:14
合計ジャッジ時間 3,825 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 6 ms
4,348 KB
testcase_06 AC 9 ms
4,348 KB
testcase_07 AC 8 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 10 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 10 ms
4,348 KB
testcase_22 AC 8 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 5 ms
4,348 KB
testcase_25 AC 7 ms
4,348 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 7 ms
4,348 KB
testcase_28 AC 7 ms
4,348 KB
testcase_29 AC 11 ms
4,348 KB
testcase_30 AC 4 ms
4,348 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 7 ms
4,348 KB
testcase_33 AC 6 ms
4,348 KB
testcase_34 AC 7 ms
4,348 KB
testcase_35 AC 7 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 5 ms
4,348 KB
testcase_38 AC 7 ms
4,348 KB
testcase_39 AC 6 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;
list<long long int> Prime(int M) {
	list<long long int>P;
	P.push_back(2);
	P.push_back(3);
	for (int i = 5; i <= M; i += 6) {
		bool flag = true;
		for (auto j : P) {
			if (i%j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i);
		flag = true;
		for (auto j : P) {
			if ((i + 2) % j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i + 2);
	}
	return P;
}

long long int N, K, Q;

int dp[100001];
int w[100001];
int v[100001];
int V;

int main() {
	ios::sync_with_stdio(false);
	cin >> N;
	for (int i = 0; i < N; i++)cin >> v[i] >> w[i];
	cin >> V;
	for (int i = 0; i < N; i++) {
		for (int j = 100000; j >= w[i]; j--) {
			dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
		}
	}
	for (int i = 1; i <= 100000; i++) {
		//cout << dp[i] << endl;
		if (dp[i] == V) {
			cout << i << endl;
			break;
		}
	}
	if (dp[N * 1000] == V) {
		cout << "inf\n";
		return 0;
	}
	for (int i = 1000*N; i>= 0; i--) {
		if (dp[i] == V) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}
0