public static void main(String[] args) { Scanner in = new Scanner(System.in); List times = new ArrayList(); String[] s = in.nextLine().split(&quot;[,\\[\\]]&quot;); String[] s1 = Arrays.copyOfRange(s, 1, s.length); int[] t = Arrays.stream(s1).mapToInt(Integer::parseInt).toArray(); for (int i = 0; i < t.length; i++) { times.add(t[i]); } System.out.println(getMaxTime(times)); } public static String getMaxTime(List<Integer> times) { StringBuilder stringBuilder = new StringBuilder(); int[][] maxTimeTable = {{2,0},{4,9},{5,0},{9,9},{5,0},{9,9}}; int pro = 0; for (int i = 0; i < maxTimeTable.length; i++) { int max = 0; if (i == 1 &amp;&amp; pro == 2) { max = maxTimeTable[i][0]; } else if (i == 1) { max = maxTimeTable[i][1]; } else max = maxTimeTable[i][0]; while (!times.contains(max)) { max--; } stringBuilder.append(max); if (i == 1 || i == 3) stringBuilder.append(&quot;:&quot;); times.remove((Integer) max); pro = max; } return stringBuilder.toString(); }