#include "bits/stdc++.h"
#include "nile.h"
using namespace std;
vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
const int MAX_W = 10;
const int MAX_D = 10;
int N = W.size(), Q = E.size();
// Group artifacts by weight: store {A, B} for each artifact
vector<vector<pair<int, int>>> weight_buckets(MAX_W + 1);
for (int i = 0; i < N; ++i) {
weight_buckets[W[i]].emplace_back(A[i], B[i]);
}
vector<long long> cost_for_d(MAX_D + 1, 0);
for (int D = 1; D <= MAX_D; ++D) {
// Make copy of all artifacts into multisets grouped by weight
vector<multiset<pair<int, int>>> available(MAX_W + 1);
for (int w = 1; w <= MAX_W; ++w) {
for (auto [a, b] : weight_buckets[w]) {
available[w].insert({b, a}); // sort by B (for pairing), keep A
}
}
long long cost = 0;
// Try to pair artifacts greedily
for (int w1 = 1; w1 <= MAX_W; ++w1) {
for (int dw = 0; dw <= D; ++dw) {
int w2 = w1 + dw;
if (w2 > MAX_W) continue;
if (w1 == w2) {
while (available[w1].size() >= 2) {
auto it1 = available[w1].begin();
auto it2 = next(it1);
cost += it1->first + it2->first;
available[w1].erase(it1);
available[w1].erase(available[w1].begin()); // re-get begin after erase
}
} else {
while (!available[w1].empty() && !available[w2].empty()) {
auto it1 = available[w1].begin();
auto it2 = available[w2].begin();
cost += it1->first + it2->first;
available[w1].erase(it1);
available[w2].erase(it2);
}
}
}
}
// Remaining unpaired items: pay A
for (int w = 1; w <= MAX_W; ++w) {
for (auto [b, a] : available[w]) {
cost += a;
}
}
cost_for_d[D] = cost;
}
// Answer queries
vector<long long> result(Q);
for (int i = 0; i < Q; ++i) {
result[i] = cost_for_d[E[i]];
}
return result;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |