結果

問題 No.3329 Only the Rightest Choice is Right!!!
コンテスト
ユーザー Rumain831
提出日時 2026-08-29 10:14:56
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 964 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,074 ms
コンパイル使用メモリ 186,076 KB
実行使用メモリ 73,984 KB
最終ジャッジ日時 2026-08-29 10:15:19
合計ジャッジ時間 8,445 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 73 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:36:7: warning: 'nw' may be used uninitialized [-Wmaybe-uninitialized]
   36 |     nw-=w[last], id=last+1;
main.cpp:25:13: note: 'nw' was declared here
   25 |   int id=0, nw;
      |             ^~

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
using P = pair<int, int>;
void chmax(P& a, P b){a=max(a, b);}

int main(void){
  int n, W; cin >> n >> W;
  vector<int> v(n), w(n);
  for(auto&x:v) cin >> x;
  for(auto&x:w) cin >> x;
  vector dp(n+1, vector<P>(W+1, P(-1e9, -1)));
  dp[n][0]=P(0, n+1);
  for(int i=n-1; i>=0; i--){
    for(int j=0; j<=W; j++){
      auto [p, id]=dp[i+1][j];
      chmax(dp[i][j], dp[i+1][j]);
      if(j!=-1&&j+w[i]<=W){
        chmax(dp[i][j+w[i]], P(p+v[i], i));
      }
    }
  }
  int id=0, nw;
  P now(-1, -1);
  for(int i=0; i<=W; i++){
    if(now<dp[0][i]){
      now=dp[0][i], id=dp[0][i].second, nw=i;
    }
  }
  vector<int> ans;
  while(now.second<n){
    auto [p, last]=now;
    ans.push_back(last+1);
    nw-=w[last], id=last+1;
    if(id>=n) break;
    now=dp[id][nw];
  }
  cout << ans.size() << endl;
  for(auto p:ans) cout << p << ' '; cout << endl;
  return 0; 
}
0