fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Solution {
  5.  
  6. public static void main(String[] args) throws Exception {
  7. int testCases;
  8. int wordSize;
  9. String word;
  10. int totalBeauty;
  11.  
  12. int tmpTotalBeauty;
  13.  
  14. Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  15. testCases = in.nextInt();
  16.  
  17. for (int count = 0; count < testCases; count++) {
  18. wordSize = in.nextInt();
  19. in.nextLine(); // flush
  20. word = in.nextLine();
  21. totalBeauty = 0;
  22. tmpTotalBeauty = 0;
  23. for (int i = 0; i < (wordSize + 1) / 2; i++) {
  24. tmpTotalBeauty += Character.getNumericValue(word.charAt(i));
  25. }
  26. totalBeauty = tmpTotalBeauty;
  27. for (int pivot = 0; pivot + (wordSize + 1) / 2 < wordSize; pivot++) {
  28. tmpTotalBeauty -= Character.getNumericValue(word.charAt(pivot));
  29. tmpTotalBeauty += Character.getNumericValue(word.charAt(pivot + (wordSize + 1) / 2));
  30. if (totalBeauty < tmpTotalBeauty) {
  31. totalBeauty = tmpTotalBeauty;
  32. }
  33. }
  34. System.out.println("Case #" + (count+1) + ": " + totalBeauty);
  35. }
  36.  
  37. //in.close();
  38.  
  39. }
  40. }
Success #stdin #stdout 0.13s 37932KB
stdin
4
4
1332
4
9583
3
616
10
1029384756
stdout
Case #1: 6
Case #2: 14
Case #3: 7
Case #4: 31