Guest User

Untitled

a guest
Jun 15th, 2019
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws Exception {
  6. PrintWriter out = new PrintWriter(System.out);
  7. FastScanner in = new FastScanner(System.in);
  8. Hop ob = new Hop();
  9. ob.solve(in, out);
  10. out.close();
  11. }
  12.  
  13. static class Hop {
  14.  
  15. public void solve(FastScanner in, PrintWriter out) {
  16. int n = in.nextInt();
  17. int[] a = new int[n + 4];
  18. int[][] dp = new int[n + 4][2];
  19. String[] s = in.next().split(",");
  20. for (int i = 0; i < s.length; ++i) a[i + 1] = Integer.parseInt(s[i]);
  21. for (int i = n; i >= 0; --i) {
  22. int x = Math.max(a[i+1] + dp[i+1][0], 2*a[i+2] + dp[i+2][0]);
  23. x = Math.max(x, 3*a[i+3] + dp[i+3][1]);
  24. dp[i][0] = Math.max(dp[i][0], x);
  25.  
  26. int y = Math.max(a[i+1] + dp[i+1][1], 2*a[i+2] + dp[i+2][1]);
  27. dp[i][1] = Math.max(dp[i][1], y);
  28. }
  29. out.println(Math.max(dp[0][0], dp[0][1]));
  30. }
  31. }
  32. }
  33.  
  34. class FastScanner
  35. {
  36. private InputStream stream;
  37. private byte[] buf = new byte[1024];
  38. private int curChar;
  39. private int numChars;
  40.  
  41. public FastScanner(InputStream stream)
  42. {
  43. this.stream = stream;
  44. }
  45.  
  46. int read()
  47. {
  48. if (numChars == -1)
  49. throw new InputMismatchException();
  50. if (curChar >= numChars)
  51. {
  52. curChar = 0;
  53. try
  54. {
  55. numChars = stream.read(buf);
  56. }
  57. catch (IOException e)
  58. {
  59. throw new InputMismatchException();
  60. }
  61.  
  62. if (numChars <= 0)
  63. return -1;
  64. }
  65. return buf[curChar++];
  66. }
  67.  
  68. boolean isSpaceChar(int c)
  69. {
  70. return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
  71. }
  72.  
  73. boolean isEndline(int c)
  74. {
  75. return c=='\n'||c=='\r'||c==-1;
  76. }
  77.  
  78. int nextInt()
  79. {
  80. return Integer.parseInt(next());
  81. }
  82.  
  83. long nextLong()
  84. {
  85. return Long.parseLong(next());
  86. }
  87.  
  88. double nextDouble()
  89. {
  90. return Double.parseDouble(next());
  91. }
  92.  
  93. String next()
  94. {
  95. int c = read();
  96. while (isSpaceChar(c))
  97. c = read();
  98. StringBuilder res = new StringBuilder();
  99. do
  100. {
  101. res.appendCodePoint(c);
  102. c = read();
  103. } while(!isSpaceChar(c));
  104. return res.toString();
  105. }
  106.  
  107. String nextLine()
  108. {
  109. int c = read();
  110. while (isEndline(c))
  111. c = read();
  112.  
  113. StringBuilder res = new StringBuilder();
  114. do
  115. {
  116. res.appendCodePoint(c);
  117. c = read();
  118. } while(!isEndline(c));
  119. return res.toString();
  120. }
  121. }
Add Comment
Please, Sign In to add comment