/
GreedyFlorist
46 lines (40 loc) · 1017 Bytes
/
GreedyFlorist
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
#include<iostream>
using namespace std;
int main(void) {
int N, K;
cin >> N >> K;
int C[N];
for(int i = 0; i < N; i++){
cin >> C[i];
}
// Sort the costs
// Have used simple bubble sort for simplicity. Better ones can be used
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
if (C[i] > C[j]) {
int temp = C[i];
C[i] = C[j];
C[j] = temp;
}
}
}
// Compute how many times the people can be used to buy all the flowers
int iterations = 0;
iterations = N/K;
if(N%K != 0) {
iterations += 1;
}
// Compute the greedy result
int result=0;
N -= 1;
int nTemp = N;
for(int i=1; i<=iterations; i++) {
int kTemp = K;
while(nTemp >= 0 && kTemp-- > 0) {
result += i*C[N--];
nTemp = N;
}
}
cout << result << "\n";
return 0;
}