import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; public class stavitel { /** * @param args the command line arguments */ /* public static void main(String[] args) throws Exception { doMain(System.in); }d * */ public static void main(String[] args) throws Exception { //doMain(new FileInputStream(new File("testData"))); doMain(System.in); } private static class BuilderData { public List> data = new ArrayList>(2); public Integer size; } private static void doMain(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; reader.readLine(); while ((line = reader.readLine()) != null) { int size = Integer.parseInt(line); BuilderData bd = new BuilderData(); for (int i = 0; i < 2; i++) { line = reader.readLine(); List ints = readLine(line); bd.data.add(ints); bd.size = size; } doStavitelInstance(bd); } reader.close(); } private static List readLine(String s) { String[] arr = s.split(" "); List list = new ArrayList(); for (String a : arr) { if (!a.isEmpty()) { list.add(Integer.parseInt(a)); } } return list; } private static void testPrint(Collection s) { for (Object any : s) { System.out.println(any); } } private static void doStavitelInstance(BuilderData data) { //min List xAxis = data.data.get(0); List yAxis = data.data.get(1); List constXAxis = new ArrayList(xAxis); List constYAxis = new ArrayList(yAxis); int volume = 0; int maxXI = 0; int maxYI = 0; while (true) { maxXI = max(xAxis); maxYI = max(yAxis); if (Math.max(xAxis.get(maxXI), yAxis.get(maxYI)) == 0) { break; } if (xAxis.get(maxXI) > yAxis.get(maxYI)) { int xAxValue = xAxis.get(maxXI); xAxis.set(maxXI, 0); int yAx = maxValue(yAxis); if (yAx >= xAxValue) { yAxis.set(max(yAxis), 0); } volume = volume + xAxValue; } else { int yAxValue = yAxis.get(maxYI); yAxis.set(maxYI, 0); int xAx = maxValue(xAxis); if (xAx >= yAxValue) { xAxis.set(max(xAxis), 0); } volume = volume + yAxValue; } } int volumeMax = 0; for (int i = 0; i < constXAxis.size(); i++) { for (int j = 0; j < constYAxis.size(); j++) { volumeMax = volumeMax + Math.min(constXAxis.get(i), constYAxis.get(j)); } } System.out.println("Minimalni budova obsahuje " + volume + " kostek, maximalni " + volumeMax + " kostek."); } private static int max(List l) { int max = 0; int maxI = 0; for (int i = 0; i < l.size(); i++) { int tmpMax = Math.max(max, l.get(i)); if (tmpMax > max) { max = tmpMax; maxI = i; } } return maxI; } private static int maxValue(List l) { return l.get(max(l)); } private static class Min { int instanceMin = Integer.MAX_VALUE; private int min(int a) { instanceMin = Math.min(a, instanceMin); return instanceMin; } private int getMin() { return instanceMin; } } }