Skip to content

Instantly share code, notes, and snippets.

@dalcon10028
Created March 28, 2020 07:37
Show Gist options
  • Save dalcon10028/ff7d707adbbd1c06f4a78d4d9c05be9e to your computer and use it in GitHub Desktop.
Save dalcon10028/ff7d707adbbd1c06f4a78d4d9c05be9e to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] stair = new int[n+1];
int[] dp = new int[n+1];
for(int i=1; i<=n; i++)
stair[i] = sc.nextInt();
dp[1] = stair[1];
if(n>=2) dp[2] = dp[1]+stair[2];
for(int i=3; i<=n; i++)
dp[i] = Math.max(dp[i-2]+stair[i], dp[i-3]+stair[i-1]+stair[i]);
System.out.print(dp[n]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment