結果

問題 No.3509 Get More Money
コンテスト
ユーザー praddumna12
提出日時 2026-04-18 05:05:40
言語 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
結果
WA  
実行時間 -
コード長 1,627 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,421 ms
コンパイル使用メモリ 342,196 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2026-04-18 05:05:52
合計ジャッジ時間 10,492 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 60
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int N;
        ll K;
        cin >> N >> K;

        vector<ll> A(N), B(N), C(N), D(N);
        for (int i = 0; i < N; i++) cin >> A[i];
        for (int i = 0; i < N; i++) cin >> B[i];
        for (int i = 0; i < N; i++) cin >> C[i];
        for (int i = 0; i < N; i++) cin >> D[i];

        // multiset: (cost, count)
        map<ll, ll> mp;
        ll total = 0;
        ll gold = 0;

        for (int i = 0; i < N; i++) {

            // add buys
            mp[A[i]] += B[i];
            total += B[i];

            // keep only best K (remove largest cost)
            while (total > K) {
                auto it = prev(mp.end()); // largest cost
                ll cost = it->first;
                ll cnt = it->second;

                ll remove = min(cnt, total - K);
                mp[cost] -= remove;
                total -= remove;

                if (mp[cost] == 0) mp.erase(cost);
            }

            // sell
            ll need = D[i];
            while (need > 0 && !mp.empty()) {
                auto it = mp.begin(); // smallest cost
                ll cost = it->first;
                ll cnt = it->second;

                ll take = min(cnt, need);

                gold += take * (C[i] - cost);

                mp[cost] -= take;
                total -= take;
                need -= take;

                if (mp[cost] == 0) mp.erase(cost);
            }
        }

        cout << gold << '\n';
    }
}
0