You are viewing a single comment's thread. Return to all comments →
Java Solution
import java.util.*; public class Solution { static int getMinimumCost(int numberOfFriends, Integer[] prices) { Arrays.sort(prices, Collections.reverseOrder()); int i = 0; int numberOfBoughtFlowers = 0; int minimumCost = 0; while (i < prices.length) { for (int j = 0; j < numberOfFriends && i < prices.length; j++) { minimumCost += (1 + numberOfBoughtFlowers) * prices[i]; i++; } numberOfBoughtFlowers++; } return minimumCost; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); final String SPACE = " "; String[] amounts = scanner.nextLine().split(SPACE); int numberOfFlowers = Integer.parseInt(amounts[0]); int numberOfFriends = Integer.parseInt(amounts[1]); Integer[] prices = new Integer[numberOfFlowers]; String[] priceItems = scanner.nextLine().split(SPACE); for (int i = 0; i < numberOfFlowers; i++) { int price = Integer.parseInt(priceItems[i]); prices[i] = price; } int minimumCost = getMinimumCost(numberOfFriends, prices); System.out.println(minimumCost); scanner.close(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Greedy Florist
You are viewing a single comment's thread. Return to all comments →
Java Solution