All problems

Problem 195

Min Cost Climbing Stairs

Easy · 10 pts · Array, Dynamic Programming

Problem

A staircase has N steps, and standing on step i costs C[i]. You may begin on step 0 or on step 1. From any step you pay its cost and then climb either one or two steps. The top is just past the last step and costs nothing. Find the minimum total cost to reach the top. Let best[i] be the cheapest cost to arrive at step i. You can arrive from one step below or two steps below, so best[i] = min(best[i - 1] + C[i - 1], best[i - 2] + C[i - 2]), with best[0] = best[1] = 0. The answer is best[N].

Input

First line: integer N. Second line: N space-separated integers.

Output

Print the minimum cost.

Constraints

2 <= N <= 1000; 0 <= C[i] <= 999

Example input

3
10 15 20

Example output

15

Explanation

With costs 10 15 20, start on step 1, pay 15, and climb two steps straight to the top. Starting on step 0 would cost at least 10 + 20 = 30, so the answer is 15.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Decode Ways Next: Longest Palindromic Subsequence