-
Notifications
You must be signed in to change notification settings - Fork 0
/
I.cpp
68 lines (55 loc) · 1.42 KB
/
I.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
using namespace std;
void bubble_sort(vector<int>& prices) {
int n = prices.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (prices[j] > prices[j + 1]) {
int temp = prices[j];
prices[j] = prices[j + 1];
prices[j + 1] = temp;
}
}
}
}
long long calculate_min_cost(const vector<int>& prices, int X) {
int N = prices.size();
long long total_cost = 0;
int i = 0;
while (i < N) {
int end = min(i + X, N);
int min_price = prices[i];
for (int j = i; j < end; ++j) {
if (prices[j] < min_price) {
min_price = prices[j];
}
}
long long sum = 0;
for (int j = i; j < end; ++j) {
sum += prices[j];
}
sum -= min_price / 2;
total_cost += sum;
i = end;
}
return total_cost;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> prices(N);
for (int i = 0; i < N; ++i) {
cin >> prices[i];
}
bubble_sort(prices);
vector<int> queries(Q);
for (int i = 0; i < Q; ++i) {
cin >> queries[i];
}
for (int i = 0; i < Q; ++i) {
int X = queries[i];
long long min_cost = calculate_min_cost(prices, X);
cout << min_