結果

問題 No.3502 GCD Knapsack
コンテスト
ユーザー Saku0512
提出日時 2026-04-17 23:42:37
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 845 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,166 ms
コンパイル使用メモリ 334,388 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2026-04-17 23:42:55
合計ジャッジ時間 10,908 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, w;
    if (!(cin >> n >> w)) return 0;

    vector<int> x(n);
    for (int i = 0; i < n; ++i) cin >> x[i];

    vector<long long> y(n);
    for (int i = 0; i < n; ++i) cin >> y[i];

    int max_x = 0;
    for (int i = 0; i < n; ++i) {
        max_x = max(max_x, x[i]);
    }

    if (w > max_x) {
        cout << 0 << '\n';
        return 0;
    }

    vector<long long> sum_y(max_x + 1, 0);
    for (int i = 0; i < n; ++i) {
        sum_y[x[i]] += y[i];
    }

    long long ans = 0;
    for (int d = w; d <= max_x; ++d) {
        long long current_sum = 0;
        for (int k = d; k <= max_x; k += d) {
            current_sum += sum_y[k];
        }
        ans = max(ans, current_sum);
    }

    cout << ans << '\n';

    return 0;
}
0