Skip to content

Instantly share code, notes, and snippets.

@hygoni
Created March 29, 2020 15:48
Show Gist options
  • Save hygoni/2749f61a631b4c9779b4939eb408e55d to your computer and use it in GitHub Desktop.
Save hygoni/2749f61a631b4c9779b4939eb408e55d to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int s[] = new int [N + 2];
int d[] = new int[N + 2];
for(int i = 1; i <= N; i++) {
s[i] = Integer.parseInt(br.readLine());
}
d[1] = s[1];
d[2] = s[1] + s[2];
for(int i = 3; i <= N; i++) {
d[i] = Math.max(d[i - 2], d[i - 3] + s[i - 1]) + s[i];
}
System.out.println(d[N]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment