Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws Exception {
- PrintWriter out = new PrintWriter(System.out);
- FastScanner in = new FastScanner(System.in);
- Hop ob = new Hop();
- ob.solve(in, out);
- out.close();
- }
- static class Hop {
- public void solve(FastScanner in, PrintWriter out) {
- int n = in.nextInt();
- int[] a = new int[n + 4];
- int[][] dp = new int[n + 4][2];
- String[] s = in.next().split(",");
- for (int i = 0; i < s.length; ++i) a[i + 1] = Integer.parseInt(s[i]);
- for (int i = n; i >= 0; --i) {
- int x = Math.max(a[i+1] + dp[i+1][0], 2*a[i+2] + dp[i+2][0]);
- x = Math.max(x, 3*a[i+3] + dp[i+3][1]);
- dp[i][0] = Math.max(dp[i][0], x);
- int y = Math.max(a[i+1] + dp[i+1][1], 2*a[i+2] + dp[i+2][1]);
- dp[i][1] = Math.max(dp[i][1], y);
- }
- out.println(Math.max(dp[0][0], dp[0][1]));
- }
- }
- }
- class FastScanner
- {
- private InputStream stream;
- private byte[] buf = new byte[1024];
- private int curChar;
- private int numChars;
- public FastScanner(InputStream stream)
- {
- this.stream = stream;
- }
- int read()
- {
- if (numChars == -1)
- throw new InputMismatchException();
- if (curChar >= numChars)
- {
- curChar = 0;
- try
- {
- numChars = stream.read(buf);
- }
- catch (IOException e)
- {
- throw new InputMismatchException();
- }
- if (numChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- boolean isSpaceChar(int c)
- {
- return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
- }
- boolean isEndline(int c)
- {
- return c=='\n'||c=='\r'||c==-1;
- }
- int nextInt()
- {
- return Integer.parseInt(next());
- }
- long nextLong()
- {
- return Long.parseLong(next());
- }
- double nextDouble()
- {
- return Double.parseDouble(next());
- }
- String next()
- {
- int c = read();
- while (isSpaceChar(c))
- c = read();
- StringBuilder res = new StringBuilder();
- do
- {
- res.appendCodePoint(c);
- c = read();
- } while(!isSpaceChar(c));
- return res.toString();
- }
- String nextLine()
- {
- int c = read();
- while (isEndline(c))
- c = read();
- StringBuilder res = new StringBuilder();
- do
- {
- res.appendCodePoint(c);
- c = read();
- } while(!isEndline(c));
- return res.toString();
- }
- }
Add Comment
Please, Sign In to add comment