fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public long minDamage(int power, int[] damage, int[] health) {
  5. int n = damage.length;
  6. List<Pair> threat = new ArrayList<>();
  7.  
  8. for (int i = 0; i < n; i++) {
  9. double timeToKill = Math.ceil((double) health[i] / power);
  10. threat.add(new Pair(timeToKill / damage[i], i));
  11. }
  12.  
  13. Collections.sort(threat, (a, b) -> Double.compare(a.threatLevel, b.threatLevel));
  14.  
  15. long totalDamage = 0, cumulativeDamage = 0;
  16. for (int d : damage) cumulativeDamage += d;
  17.  
  18. for (int i = 0; i < n; i++) {
  19. int idx = threat.get(i).index;
  20. int timeToKill = (int) Math.ceil((double) health[idx] / power);
  21. totalDamage += cumulativeDamage * timeToKill;
  22. cumulativeDamage -= damage[idx];
  23. }
  24.  
  25. return totalDamage;
  26. }
  27.  
  28. class Pair {
  29. double threatLevel;
  30. int index;
  31.  
  32. Pair(double threatLevel, int index) {
  33. this.threatLevel = threatLevel;
  34. this.index = index;
  35. }
  36. }
  37.  
  38. public static void main(String[] args) {
  39. int power = 10;
  40. int[] damage = {3, 5, 2, 4};
  41. int[] health = {20, 15, 10, 25};
  42.  
  43. Main game = new Main();
  44. long result = game.minDamage(power, damage, health);
  45.  
  46. System.out.println("Minimum total damage: " + result);
  47. }
  48. }
  49.  
Success #stdin #stdout 0.1s 53864KB
stdin
Standard input is empty
stdout
Minimum total damage: 63