結果

問題 No.527 ナップサック容量問題
ユーザー TangentDayTangentDay
提出日時 2017-06-09 23:04:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,193 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 86,172 KB
実行使用メモリ 43,072 KB
最終ジャッジ日時 2023-10-24 00:10:39
合計ジャッジ時間 3,073 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,056 KB
testcase_01 AC 4 ms
5,584 KB
testcase_02 AC 3 ms
4,792 KB
testcase_03 AC 4 ms
5,584 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 21 ms
19,576 KB
testcase_06 AC 42 ms
37,264 KB
testcase_07 AC 34 ms
30,664 KB
testcase_08 AC 18 ms
17,200 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 43 ms
37,792 KB
testcase_11 AC 30 ms
27,496 KB
testcase_12 AC 21 ms
20,896 KB
testcase_13 AC 43 ms
38,584 KB
testcase_14 AC 17 ms
16,144 KB
testcase_15 AC 26 ms
23,536 KB
testcase_16 AC 4 ms
5,584 KB
testcase_17 AC 21 ms
20,104 KB
testcase_18 AC 24 ms
22,480 KB
testcase_19 AC 4 ms
5,848 KB
testcase_20 AC 17 ms
16,936 KB
testcase_21 AC 50 ms
43,072 KB
testcase_22 AC 33 ms
29,872 KB
testcase_23 AC 49 ms
42,016 KB
testcase_24 AC 11 ms
11,392 KB
testcase_25 AC 32 ms
29,080 KB
testcase_26 AC 29 ms
26,440 KB
testcase_27 AC 29 ms
26,704 KB
testcase_28 AC 26 ms
23,536 KB
testcase_29 AC 49 ms
42,808 KB
testcase_30 AC 7 ms
7,432 KB
testcase_31 AC 26 ms
20,368 KB
testcase_32 AC 32 ms
24,328 KB
testcase_33 AC 26 ms
21,160 KB
testcase_34 AC 32 ms
25,120 KB
testcase_35 AC 27 ms
25,120 KB
testcase_36 AC 3 ms
5,056 KB
testcase_37 AC 21 ms
19,576 KB
testcase_38 AC 25 ms
24,064 KB
testcase_39 AC 24 ms
22,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) c.begin(), c.end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

int main() {
    int n;
    cin >> n;
    VI v(n), w(n);
    REP(i,n) cin >> v[i] >> w[i];
    int p;
    cin >> p;
    int m = 100001;
    VVI dp(n+1, VI(m));
    REP(i,n) REP(j,m){
        dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        if (j + w[i] < m) dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j] + v[i]);
    }
    FOR(j,1,m){
        if (dp[n][j] == p){
            cout << j << endl;
            break;
        }
    }
    if (dp[n][m-1] == p) cout << "inf" << endl;
    else{
        FORR(j,m-1,0){
            if (dp[n][j] == p){
                cout << j << endl;
                break;
            }
        }
    }
}
0