@@ -0,0 +1,1214 @@
+/* Sample program illustrating input/output methods */%0Aimport java.util.*;%0A%0Aclass Solution %7B%0A  // time = O(n log n)%0A  // space = O(k)%0A  public static int buyFlowersOkay(int%5B%5D arr, int k) %7B%0A    int%5B%5D cust = new int%5Bk%5D;%0A    Arrays.sort(arr);%0A    int i = arr.length - 1;%0A    int rnd = 1;%0A    while (i %3E= 0) %7B%0A      for (int j = 0; j %3C k; j++) %7B%0A        int price = (i %3E= 0) ? arr%5Bi%5D : 0;%0A        cust%5Bj%5D += price * rnd;%0A        i--;%0A      %7D%0A      rnd++;%0A    %7D%0A%0A    int sum = 0;%0A    for (int s : cust) %7B%0A      sum += s;%0A    %7D%0A    return sum;%0A  %7D%0A%0A  // time = O(n log n)%0A  // space = O(1)%0A  public static int buyFlowersBetter(int%5B%5D arr, int k) %7B%0A    int sum = 0;%0A    int rnd = 1;%0A    int ktmp = k;%0A    Arrays.sort(arr);%0A    for (int i = arr.length - 1; i %3E= 0; i--) %7B%0A      sum += arr%5Bi%5D * rnd;%0A      ktmp--;%0A      if (ktmp == 0) %7B%0A        rnd += 1;%0A        ktmp = k;%0A      %7D%0A    %7D%0A    return sum;%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    Scanner in = new Scanner(System.in);%0A    int N, K;%0A    N = in.nextInt();%0A    K = in.nextInt();%0A%0A    int%5B%5D C = new int%5BN%5D;%0A    for (int i = 0; i %3C N; i++) %7B%0A      C%5Bi%5D = in.nextInt();%0A    %7D%0A%0A    int result = buyFlowersBetter(C, K);%0A    System.out.println(result);%0A  %7D%0A%7D%0A

add solution to greedy-florist