-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathGreedyFlorist.java
More file actions
34 lines (28 loc) 路 681 Bytes
/
Copy pathGreedyFlorist.java
File metadata and controls
34 lines (28 loc) 路 681 Bytes
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
/* Sample program illustrating input/output methods */
import java.util.*;
class Solution{
public static void main( String args[] ){
// helpers for input/output
Scanner in = new Scanner(System.in);
int N, K;
N = in.nextInt();
K = in.nextInt();
int C[] = new int[N];
for(int i=0; i<N; i++){
C[i] = in.nextInt();
}
Arrays.sort(C);
int result = 0;
int x =1;
int j=0;
for(int i=N-1;i>=0;i--){
result = result + x*C[i];
j++;
if(j==K){
x++;
j=0;
}
}
System.out.println( result );
}
}