ZZULIOJ 1146: Eat candy, Java
Question description
HOHO, finally won all the candies from Speakless. Gardon has a special hobby when eating candies. He doesn't like to eat the same candies twice in a row. He likes to eat one type A candy first and change it to another one next time. Each flavor, eat a candy of type B, that's it; but Gardon doesn't know if there is an order of eating candies that allows him to eat all the candies? Please write a program to help calculate it.
enter
The first line has an integer T, followed by T groups of data, each group of data occupies 2 lines. The first line is an integer N (0 < N <= 1000000), indicating the type of candy. The second line is N numbers, representing the number of each type of candy Mi (0 < Mi <= 109).
output
For each set of data, output a row containing a "Yes" or "No".
Sample inputCopy
2
3
4 1 1
5
5 4 3 2 1
Sample outputCopy
No
Yes
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bf.readLine());
for (int i = 0; i < T; i++) {
int n = Integer.parseInt(bf.readLine());
int[] a = new int[n];
String[] str = bf.readLine().split(" ");
for (int j = 0; j < n; j++) {
a[j] = Integer.parseInt(str[j]);
}
int mx = 0, sum = 0;
for (int c : a) {
sum += c;
mx = Math.max(mx, c);
}
bw.write(mx <= sum - mx + 1 ? "Yes\n" : "No\n");
}
bw.close();
}
}