結果
| 問題 | No.3509 Get More Money |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 22:49:52 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 2,000 ms |
| コード長 | 2,463 bytes |
| 記録 | |
| コンパイル時間 | 1,399 ms |
| コンパイル使用メモリ | 178,920 KB |
| 実行使用メモリ | 21,632 KB |
| 最終ジャッジ日時 | 2026-04-18 22:50:18 |
| 合計ジャッジ時間 | 12,608 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge3_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 60 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
void solve() {
int N;
long long K;
cin >> N >> K;
vector<long long> 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];
// Map stores {cost: count_of_jewels_available_at_this_cost}
map<long long, long long> pq;
long long total_items = 0;
long long profit = 0;
for (int i = 0; i < N; i++) {
// 1. Add up to B[i] buying options
if (B[i] > 0) {
pq[A[i]] += B[i];
total_items += B[i];
}
// 2. Greedily sell up to D[i] items at price C[i]
long long to_sell = D[i];
long long fake_buys = 0;
while (to_sell > 0 && !pq.empty()) {
auto it = pq.begin();
long long cost = it->first;
long long count = it->second;
// Stop if the cheapest available cost is strictly greater or equal to selling price
if (cost >= C[i]) {
break;
}
long long take = min(to_sell, count);
profit += take * (C[i] - cost);
fake_buys += take;
to_sell -= take;
it->second -= take;
if (it->second == 0) {
pq.erase(it);
}
}
// Add back fake buys to allow future upgrading of the sales prices
if (fake_buys > 0) {
pq[C[i]] += fake_buys;
// Notice total_items doesn't increase here because the fake_buys
// directly replace the actual buys we pulled out above.
}
// 3. Ensure we don't violate the storage capacity constraint K
while (total_items > K) {
auto it = prev(pq.end()); // Look at the elements with the highest cost
long long count = it->second;
long long remove_cnt = min(total_items - K, count);
total_items -= remove_cnt;
it->second -= remove_cnt;
if (it->second == 0) {
pq.erase(it);
}
}
}
cout << profit << "\n";
}
int main() {
// Fast I/O
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
if (cin >> T) {
while (T--) {
solve();
}
}
return 0;
}