Can someone please tell me why i am getting WA verdict, I’ve run this on my IDE(Netbeans 9.0) and the code outputs correct for given sample test case. But when i submit it shows Wrong Answer…

Question:(Click the link below)
Problem statement

Logic I worked out:
I’m maintaining an ArrayList for powers, in each query minimum power will be searched for the target to battle with, it’s then removed from array list updating the power of fighter, this is iterated until unless either list is empty or remaining powers are greater than fighter’s.

My code:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
    	static class FastScanner{
        BufferedReader br ;
        StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        String next(){
            while(st==null || !st.hasMoreElements()){
                try {
                    st = new StringTokenizer(br.readLine()) ;
                } catch (IOException ex) {
                    System.out.println("error") ;
            return st.nextToken() ;
        int nextInt(){
            return Integer.parseInt(next()) ;
        Long nextLong() {
            return Long.parseLong(next());
    //main program
	public static void main (String[] args) throws java.lang.Exception
       FastScanner s = new FastScanner();

        int t = s.nextInt();

        while (t-- > 0) {
            int n = s.nextInt();
            int q = s.nextInt();

            ArrayList<Long> pwr = new ArrayList<Long>();
            while (n-- > 0) {
            ArrayList<Long> army;
            while (q-- > 0) {
                army = (ArrayList<Long>) pwr.clone();
                long x = s.nextLong();
                long y; //here minimum power of the list army will be stored
                int kills = 0;
                while (!army.isEmpty()) {
                    y = Collections.min(army);
                    if (x > y) {
                        x = 2 * (x - y);
                    } else {



I know there is some learning hidden in this issue as I’m a newbie…kindly help me grasp that:

  • Why WA?
  • What should be done(preferably the code in JAVA to illustrate if possible)?

–Thanks in advance.