I was doing this wayy too complicatedly before using an ArrayList and a counter to see if I could greedily find the appropriate match or not. The easiest way I realized mid coding process was to just let the transactions go through and then check at the end if everyone ends up with a positive or 0 balance.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11679 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNextInt()) { int B = sc.nextInt(); int N = sc.nextInt(); if (B == N && B == 0) break; int[] reserves = new int[B]; for (int i = 0; i < reserves.length; i++) reserves[i] = sc.nextInt(); for (int i = 0; i < N; i++) { int from = sc.nextInt() - 1; int to = sc.nextInt() - 1; int amt = sc.nextInt(); reserves[from] -= amt; reserves[to] += amt; } if (isCool(reserves)) { out.println("S"); } else { out.println("N"); } } out.close(); sc.close(); } private static boolean isCool(int[] reserves) { for (int i = 0; i < reserves.length; i++) { if (reserves[i] < 0) return false; } return true; } }