UVA 661 – Blowing Fuses

Blank line after every case. :[

import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P661 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int count = 1;
    while (sc.hasNextInt()) {

      int N = sc.nextInt();
      int M = sc.nextInt();
      int C = sc.nextInt();

      if (N == 0)
        break;

      long[] power = new long[N];
      for (int i = 0; i < N; i++)
        power[i] = sc.nextInt();

      boolean[] on = new boolean[N];
      long current = 0;
      long max = 0;
      for (int i = 0; i < M; i++) {
        int tmp = sc.nextInt() - 1;
        if (on[tmp]) {
          on[tmp] = false;
          current -= power[tmp];
        } else {
          on[tmp] = true;
          current += power[tmp];
          max = Math.max(max, current);
        }
      }

      out.println("Sequence " + count);
      if (max > C) {
        out.println("Fuse was blown.");
      } else {
        out.println("Fuse was not blown.");
        out.println("Maximal power consumption was " + max + " amperes.");
      }
      out.println();
      count++;
    }

    out.close();
    sc.close();
  }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.