fork download
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.Locale;
  4. import java.util.StringTokenizer;
  5.  
  6. public class TaskC {
  7. private final InputReader reader;
  8. private final OutputWriter writer;
  9.  
  10. public TaskC(InputReader reader, OutputWriter writer) {
  11. this.reader = reader;
  12. this.writer = writer;
  13. }
  14.  
  15. public static void main(String[] args) {
  16. InputReader reader = new InputReader(System.in);
  17. OutputWriter writer = new OutputWriter(System.out);
  18. new TaskC(reader, writer).run();
  19. writer.writer.flush();
  20. }
  21.  
  22. int[] A;
  23. int[] tp;
  24.  
  25. class Pair {
  26. long x, y;
  27. Pair(long x, long y) {
  28. this.x = x;
  29. this.y = y;
  30. }
  31. }
  32.  
  33. long get(int l, int r) {
  34. if (l > r)
  35. return 1;
  36. long cur = A[l];
  37. long sum = 0;
  38. for (int i = l + 1; i <= r; i++) {
  39. if (tp[i] == 1) {
  40. sum += cur;
  41. cur = A[i];
  42. } else {
  43. cur *= A[i];
  44. }
  45. }
  46. return sum + cur;
  47. }
  48.  
  49. public void run() {
  50. char[] buf = reader.next().toCharArray();
  51. A = new int[buf.length / 2 + 1];
  52. int n = A.length;
  53. for (int i = 0; i < n; i++)
  54. A[i] = buf[2 * i] - '0';
  55. tp = new int[n];
  56. tp[0] = 0;
  57. int stars = 0;
  58. for (int i = 1; i < n; i++) {
  59. tp[i] = buf[2 * i - 1] == '+' ? 1 : 0;
  60. stars += 1 - tp[i];
  61. }
  62. stars++;
  63. int[] pos = new int[stars * 2 + 2];
  64. int pt = 0;
  65. for (int i = 0; i < n; i++) {
  66. if (tp[i] == 0) {
  67. pos[pt++] = i;
  68. pos[pt++] = i - 1;
  69. if (pos[pt - 1] < 0)
  70. pos[pt - 1] = 0;
  71. }
  72. }
  73. pos[pt++] = 0;
  74. pos[pt++] = n - 1;
  75.  
  76. Arrays.sort(pos);
  77. pt = 1;
  78. for (int i = 1; i < pos.length; i++)
  79. if (pos[i] != pos[pt - 1])
  80. pos[pt++] = pos[i];
  81. pos = Arrays.copyOfRange(pos, 0, pt);
  82. long best = 0;
  83. for (int l : pos) {
  84. for (int r : pos) {
  85. if (l > r)
  86. continue;
  87. long val = get(l, r);
  88. long sum = 0, cur = (l == 0) ? val : A[0];
  89. for (int i = (l == 0) ? r + 1 : 1; i < n; i++) {
  90. long w = (i == l) ? val : A[i];
  91. if (tp[i] == 1) {
  92. sum += cur;
  93. cur = w ;
  94. } else {
  95. cur *= w;
  96. }
  97. if (i == l)
  98. i = r;
  99. }
  100. sum += cur;
  101. best = Math.max(best, sum);
  102. }
  103. }
  104. writer.printf("%d\n", best);
  105. }
  106.  
  107.  
  108. static class InputReader {
  109. public BufferedReader reader;
  110. public StringTokenizer tokenizer;
  111.  
  112. public InputReader(InputStream stream) {
  113. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  114. tokenizer = null;
  115. }
  116.  
  117. public String next() {
  118. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  119. try {
  120. tokenizer = new StringTokenizer(reader.readLine());
  121. } catch (IOException e) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. return tokenizer.nextToken();
  126. }
  127.  
  128. public int nextInt() {
  129. return Integer.parseInt(next());
  130. }
  131.  
  132. public double nextDouble() {
  133. return Double.parseDouble(next());
  134. }
  135.  
  136. public long nextLong() {
  137. return Long.parseLong(next());
  138. }
  139. }
  140.  
  141. static class OutputWriter {
  142. public PrintWriter writer;
  143.  
  144. OutputWriter(OutputStream stream) {
  145. writer = new PrintWriter(stream);
  146. }
  147.  
  148. public void printf(String format, Object... args) {
  149. writer.print(String.format(Locale.ENGLISH, format, args));
  150. }
  151. }
  152. }
  153.  
  154.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:6: error: class TaskC is public, should be declared in a file named TaskC.java
public class TaskC {
       ^
1 error
stdout
Standard output is empty