結果
問題 | No.617 Nafmo、買い出しに行く |
ユーザー |
![]() |
提出日時 | 2019-02-24 15:17:28 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 459 ms / 2,000 ms |
コード長 | 1,164 bytes |
コンパイル時間 | 1,372 ms |
コンパイル使用メモリ | 161,364 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-23 00:49:57 |
合計ジャッジ時間 | 12,727 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h>using namespace std;// const int LIMIT = 1 << 3;// const int DIGIT = 3;const int LIMIT = 1048576;const int DIGIT = 20;int main() {// 1. 入力情報取得.int N, K;cin >> N >> K;int A[DIGIT] = {};for(int i = 0; i < N; i++) cin >> A[i];// 2. 商品の重さの合計を計算.// N の 上限が, 2 の 20乗 = 1048576 なので, 全部確認できそう.int ans = 0;for(int i = 0; i < LIMIT; i++){// 2-1. i を 2進数表記.stringstream ss;ss << static_cast<std::bitset<DIGIT>>(i);string bit = ss.str();// cout << bit << endl;// 2-2. 各商品について, 以下のルールで読み替える.// bit の 各桁が 1 なら, 購入する// bit の 各桁が 0 なら, 購入しないint total = 0;for(int d = 0; d < DIGIT; d++) total += (bit[d] - '0') * A[d];// 2-3. 商品の重さの合計について, 最大値を更新.if(total <= K) ans = max(ans, total);}// 3. 出力 ~ 終了.cout << ans << endl;return 0;}