fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.IntStream;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. final class Order {
  11. private final int andyTip;
  12. private final int bobTip;
  13.  
  14. public Order(int andyTip, int bobTip) {
  15. this.andyTip = andyTip;
  16. this.bobTip = bobTip;
  17. }
  18.  
  19. public int getAndyTip() {
  20. return andyTip;
  21. }
  22.  
  23. public int getBobTip() {
  24. return bobTip;
  25. }
  26.  
  27. public int getDifference() {
  28. return andyTip - bobTip;
  29. }
  30. }
  31. class Ideone
  32. {
  33. public static void main(String[] args) {
  34.  
  35. try (Scanner sc = new Scanner(System.in)) {
  36. int n = sc.nextInt(); // Total number of orders
  37. int x = sc.nextInt(); // Maximum orders Andy can take
  38. int y = sc.nextInt(); // Maximum orders Bob can take
  39.  
  40. List<Integer> andyTips = IntStream.range(0, n).map(i -> sc.nextInt()).boxed().collect(Collectors.toList());
  41. List<Integer> bobTips = IntStream.range(0, n).map(i -> sc.nextInt()).boxed().collect(Collectors.toList());
  42.  
  43. List<Order> orders = IntStream.range(0, n)
  44. .mapToObj(i -> new Order(andyTips.get(i), bobTips.get(i)))
  45. .collect(Collectors.toList());
  46.  
  47. orders.sort(Comparator.comparingInt(Order::getDifference).reversed());
  48.  
  49. long maxTotalTip = 0;
  50. int andyCount = Math.min(n, x);
  51. int bobCount = n - andyCount;
  52.  
  53. long andyPortion = orders.stream().limit(andyCount).mapToLong(Order::getAndyTip).sum();
  54. long bobPortion = orders.stream().skip(andyCount).mapToLong(Order::getBobTip).sum();
  55. if (bobCount <= y) {
  56. maxTotalTip = andyPortion + bobPortion;
  57. }
  58. for (int i = andyCount - 1; i >= 0; i--) {
  59. Order orderToMove = orders.get(i);
  60.  
  61. // Simulate moving the order from Andy's list to Bob's.
  62. andyPortion -= orderToMove.getAndyTip();
  63. bobPortion += orderToMove.getBobTip();
  64. bobCount++;
  65. if (bobCount <= y) {
  66. maxTotalTip = Math.max(maxTotalTip, andyPortion + bobPortion);
  67. } else {
  68. break;
  69. }
  70. }
  71.  
  72. System.out.println(maxTotalTip);
  73. }
  74. }
  75. }
Success #stdin #stdout 0.16s 57088KB
stdin
5 3 3
1 2 3 4 5
5 4 3 2 1
stdout
21