P2904 [USACO08MAR] River Crossing S

Description

Farmer John is herding his N cows (1

Input Format

\* Line 1: Two space-separated integers: N and M \* Lines 2..N+1: Line i+1 contains a single integer: M\_i

Output Format

\* Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.

Explanation/Hint

There are five cows. Farmer John takes 10 minutes to cross the river alone, 13 with one cow, 17 with two cows, 23 with three, 123 with four, and 124 with all five. Farmer John can first cross with three cows (23 minutes), then return (10 minutes), and then cross with the last two (17 minutes). 23+10+17 = 50 minutes total.