結果

問題 No.617 Nafmo、買い出しに行く
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-18 10:54:37
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 752 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 51,612 KB
最終ジャッジ日時 2023-08-22 05:16:16
合計ジャッジ時間 1,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:7:3: error: ‘vector’ was not declared in this scope
   vector<int> a(n);
   ^~~~~~
main.cpp:7:3: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
main.cpp:3:1:
+#include <vector>
 using namespace std;
main.cpp:7:3:
   vector<int> a(n);
   ^~~~~~
main.cpp:7:10: error: expected primary-expression before ‘int’
   vector<int> a(n);
          ^~~
main.cpp:8:41: error: ‘a’ was not declared in this scope
   for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
                                         ^
main.cpp:10:10: error: expected primary-expression before ‘bool’
   vector<bool> dp(K+1);
          ^~~~
main.cpp:11:3: error: ‘dp’ was not declared in this scope
   dp[0] = true;
   ^~
main.cpp:13:12: error: expected primary-expression before ‘bool’
     vector<bool> ndp(K+1);
            ^~~~
main.cpp:16:7: error: ‘ndp’ was not declared in this scope
       ndp[k]        = true; // i番目を選ばない
       ^~~
main.cpp:17:14: error: ‘a’ was not declared in this scope
       if(k + a[i] <= K) {
              ^
main.cpp:21:10: error: ‘ndp’ was not declared in this scope
     dp = ndp;
          ^~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

int main(void) {
  int n, K; scanf("%d%d", &n, &K);
  vector<int> a(n);
  for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
  // dp[i番目を選ぶか選ばないか悩んでいる状態でのこれまでの重さ] := 可否
  vector<bool> dp(K+1);
  dp[0] = true;
  for(int i=0; i<n; ++i) {
    vector<bool> ndp(K+1);
    for(int k=0; k<=K; ++k) { // K を含めるのを忘れないように
      if(!dp[k]) { continue; }
      ndp[k]        = true; // i番目を選ばない
      if(k + a[i] <= K) {
        ndp[k+a[i]] = true; // i番目を選ぶ
      }
    }
    dp = ndp;
  }
  for(int k=K; k>=0; --k) {
    if(dp[k]) {
      printf("%d\n", k);
      return 0;
    }
  }
  throw;
}
0