class Solution { public int magicTower(int[] nums) { PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((num1, num2) -> num1 - num2); int ans = 0; long hp = 1; long last = 0; for (int num : nums) { hp += num; if (num < 0) { priorityQueue.offer(num); if (hp <= 0) { Integer moveNum = priorityQueue.poll(); last += moveNum; hp -= moveNum; ans++; } } } hp += last; return hp <= 0 ? -1 : ans; } }