import java.io.*; import java.util.*; /** * This Wrong Answer implementation uses a greedy approach to decide which * product to send to which warehouse. * * Choose the (product, warehouse) pair such that if the product is not allowed * to go to its best warehouse, the cost of sending it to its second best warehouse * has the greatest difference. * * @author Finn Lidbetter */ public class Greedy2 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int w = Integer.parseInt(s[0]); int p = Integer.parseInt(s[1]); long[][] products = new long[w][p]; for (int i=0; i> sortedCosts = new ArrayList<>(); for (int i=0; i costs = new TreeSet<>(); for (int j=0; j 1) { WarehouseCost second = sortedCosts.get(i).higher(sortedCosts.get(i).first()); long diff = second.cost - currBest.cost; if (best == null || diff > worstDiff || (diff == worstDiff && currBest.cost < best.cost)) { worstDiff = diff; bestProduct = i; best = currBest; } } else { if (best == null || currBest.cost < best.cost) { bestProduct = i; best = currBest; } } } } total += best.cost; chosen[bestProduct] = true; for (int i=0; i { int warehouseNumber; long cost; public WarehouseCost(int id, long c) { warehouseNumber = id; cost = c; } public int compareTo(WarehouseCost w2) { if (cost == w2.cost) { return warehouseNumber - w2.warehouseNumber; } return Long.compare(cost, w2.cost); } }