*Not able to understand the failing case when all E i+ Ri > Sum(E)i* *Can someone help with this> I've written below code*
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws Exception { ModScanner ms = new ModScanner(); int t = ms.nextInt(); int tc = 0; while (t-- > 0) { int n = ms.nextInt(); PriorityQueue<Toy> toysByScore = new PriorityQueue<>(Comparator.comparing( Toy::getTotal).reversed()); int[] e = new int[n]; int[] r = new int[n]; int maxEnjoy = 0; for (int i = 0; i < n; i++) { e[i] = ms.nextInt(); r[i] = ms.nextInt(); maxEnjoy += e[i]; } int removed = 0; int removedWhenFinite = 0; int totalEnjoy = maxEnjoy; int currEnjoy = maxEnjoy; for (int i = 0; i < n; i++) { toysByScore.add(new Toy(e[i], r[i])); currEnjoy += e[i]; while (!toysByScore.isEmpty() && toysByScore.peek().getTotal() > totalEnjoy) { Toy badToy = toysByScore.poll(); removed++; totalEnjoy -= badToy.getEnjoy(); currEnjoy -= badToy.getEnjoy() * 2; } if (currEnjoy > maxEnjoy) { maxEnjoy = currEnjoy; removedWhenFinite = removed; } } String longestTime; if (!toysByScore.isEmpty()) { longestTime = "INDEFINITELY"; } else { removed = removedWhenFinite; longestTime = String.valueOf(maxEnjoy); } System.out.println("Case #" + ++tc + ": " + removed + " " + longestTime); } } } class Toy { private final int enjoy; private final int remem; public Toy(int enjoy, int remem) { this.enjoy = enjoy; this.remem = remem; } public int getTotal() { return enjoy + remem; } public int getEnjoy() { return enjoy; } } class ModScanner { BufferedReader br; StringTokenizer st; public ModScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() throws Exception { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } String nextString() throws Exception { return br.readLine(); } int nextInt() throws Exception, Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } } -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-code+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/c5904a88-ae60-49d0-8f55-13c3f91ebe86n%40googlegroups.com.