結果

問題 No.527 ナップサック容量問題
ユーザー yukudo
提出日時 2017-06-09 23:06:58
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 1,408 ms
コンパイル使用メモリ 160,360 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-22 17:31:25
合計ジャッジ時間 2,688 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int nextInt()’:
main.cpp:10:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |   int x; scanf("%d", &x); return x;
      |          ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
template<class T>bool chmin(T&a,const T&b){return a>b?(a=b,true):false;}
template<class T>bool chmax(T&a,const T&b){return a<b?(a=b,true):false;}

int nextInt() {
  int x; scanf("%d", &x); return x;
}

int v[128], w[128];

const int INF = 1001001001;
const int MAX_N = 100010;
int dp[MAX_N];

int main2() {
  int N = nextInt();
  REP(i, N) {
    v[i] = nextInt();
    w[i] = nextInt();
  }
  int V = nextInt();

  REP(i, MAX_N) dp[i] = 0;

  REP(i, N) {
    for (int j = MAX_N - 1; j - w[i] >= 0; j--) {
      chmax(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  int mi = -1, ma = -1;
  for (int i = 1; i < MAX_N; i++) if (dp[i] == V) {
    if (mi == -1) mi = i;
    ma = i;
  }
  cout << mi << endl;
  if (ma >= MAX_N-1) cout << "inf" << endl; else cout << ma << endl;

  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws) main2();
  return 0;
}
0