Someone solve please in java?

A competition consists of N races. For each i (1 ≤ iN),

  • Alice finished the i-th race in Ai minutes.
  • Bob finished it in Bi minutes.
  • The player with the smallest sum of finish times wins.
  • If this total time is the same for Alice and for Bob, a draw is declared.

The rules of the competition allow each player to choose a race which will not be counted towards their total time.

  • That is, Alice may choose an index x and her finish time in the race with this index will be considered zero;
  • Similarly, Bob may choose an index y and his finish time in the race with this index will be considered zero.
  • Note: x can be different from y; the index chosen by Alice does not affect Bob’s total time or vice versa.

Chef, as the judge, needs to announce the result of the competition. He knows that both Alice and Bob play optimally and will always choose the best option. Please help Chef determine the result!
my written code is below:

import java.util.*;

class Codechef {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int t = read.nextInt();
for (int i = 0; i < t; i++) {
ArrayList a = new ArrayList();
int n = read.nextInt();
for (int j = 1; j <= n; j++) {
int ele = read.nextInt();
a.add(ele);
}

        ArrayList<Integer> b = new ArrayList<Integer>();
        for (int k = 1; k <= n; k++) {
            int ele = read.nextInt();
            b.add(ele);
        }

        // Compute maximum values excluding the chosen races
        int maxa = Integer.MAX_VALUE;
        int maxaIndex = -1;
        int maxb = Integer.MAX_VALUE;
        int maxbIndex = -1;
        for (int f = 0; f < a.size(); f++) {
            if (a.get(f) < maxa) {
                maxa = a.get(f);
                maxaIndex = f;
            }
            if (b.get(f) < maxb) {
                maxb = b.get(f);
                maxbIndex = f;
            }
        }

        // Update maximum values if chosen races have smaller times
        if (maxaIndex != -1 && a.get(maxaIndex) == 0) {
            maxa = Integer.MAX_VALUE;
            for (int f = 0; f < a.size(); f++) {
                if (f != maxaIndex && a.get(f) < maxa) {
                    maxa = a.get(f);
                }
            }
        }
        if (maxbIndex != -1 && b.get(maxbIndex) == 0) {
            maxb = Integer.MAX_VALUE;
            for (int f = 0; f < b.size(); f++) {
                if (f != maxbIndex && b.get(f) < maxb) {
                    maxb = b.get(f);
                }
            }
        }

        // Compute total times
        int suma = 0;
        int sumb = 0;
        for (int s = 0; s < a.size(); s++) {
            if (s != maxaIndex) {
                suma += a.get(s);
            }
            if (s != maxbIndex) {
                sumb += b.get(s);
            }
        }

        // Compare total times and output result
        if (suma == sumb) {
            System.out.println("Draw");
        } else if (suma < sumb) {
            System.out.println("Alice");
        } else {
            System.out.println("Bob");
        }
    }
}

}

Hello, @muhammadaftab – welcome to CodeChef.

I didn’t parse your entire code submission, but this part jumped out at me…

        int maxa = Integer.MAX_VALUE;
        int maxaIndex = -1;
        int maxb = Integer.MAX_VALUE;
        int maxbIndex = -1;
        for (int f = 0; f < a.size(); f++) {
            if (a.get(f) < maxa) {
                maxa = a.get(f);
                maxaIndex = f;
            }
            if (b.get(f) < maxb) {
                maxb = b.get(f);
                maxbIndex = f;
            }
        }

This seems to be finding the minimum element in each list. Is that really what you want?

For example, if Alice = { 1, 2, 10 } and Bob = { 2, 3, 4 }, the optimal solution is for Alice to remove the 10-minute race and for Bob to remove the 4-minute race. But instead it looks like your algorithm removes 1 from Alice and 2 from Bob.

I hope this helps! --Sbat