結果

問題 No.3509 Get More Money
コンテスト
ユーザー 왕지후
提出日時 2026-04-18 12:32:11
言語 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  
実行時間 -
コード長 2,095 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,046 ms
コンパイル使用メモリ 344,668 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2026-04-18 12:32:30
合計ジャッジ時間 14,707 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 60
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using int64 = long long;
using i128 = __int128_t;

static void print_i128(i128 x) {
    if (x == 0) {
        cout << 0 << '\n';
        return;
    }
    string s;
    while (x > 0) {
        int d = (int)(x % 10);
        s.push_back(char('0' + d));
        x /= 10;
    }
    reverse(s.begin(), s.end());
    cout << s << '\n';
}

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

    int T;
    cin >> T;

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

        vector<int64> 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];

        // key = buy cost, value = number of unsold candidate jewels
        map<int64, int64> mp;
        int64 sz = 0;
        i128 ans = 0;

        for (int i = 0; i < N; i++) {
            // 1) add today's buy candidates
            mp[A[i]] += B[i];
            sz += B[i];

            // 2) sell up to D[i], always from cheapest profitable ones
            int64 canSell = D[i];
            while (canSell > 0 && !mp.empty()) {
                auto it = mp.begin();
                int64 cost = it->first;
                int64 cnt  = it->second;

                if (cost >= C[i]) break;

                int64 take = min(canSell, cnt);
                ans += (i128)take * (C[i] - cost);

                canSell -= take;
                sz -= take;
                it->second -= take;
                if (it->second == 0) mp.erase(it);
            }

            // 3) keep at most K cheapest unsold ones for future
            while (sz > K) {
                auto it = prev(mp.end()); // largest cost
                int64 cnt = it->second;
                int64 remove = min<int64>(cnt, sz - K);

                it->second -= remove;
                sz -= remove;
                if (it->second == 0) mp.erase(it);
            }
        }

        print_i128(ans);
    }

    return 0;
}
0