Problem:
Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942

Solution in Rust (1.42.0) (judge verdict: Accepted)

use whiteread::parse_line;

fn calc(n: usize, p: &mut Vec<usize>, qhsd: [usize; 4]) -> usize {
    if p[n] == std::usize::MAX {
        let mut res = std::usize::MAX;
        if 2 <= n && n <= 4 {
            res = res.min(calc(n - 1, p, qhsd) + qhsd[0]);
            res = res.min(calc(n - 2, p, qhsd) + qhsd[1]);
        }
        if n >= 4 {
            res = res.min(calc(n - 4, p, qhsd) + qhsd[2]);
        }
        if n >= 8 {
            res = res.min(calc(n - 8, p, qhsd) + qhsd[3]);
        }
        p[n] = res;
    }
    p[n]
}

fn main() {
    let (q, h, s, d): (usize, usize, usize, usize) = parse_line().unwrap();
    let n: usize = parse_line().unwrap();
    let mut p: Vec<usize> = vec![std::usize::MAX; 10];
    p[0] = 0;
    p[1] = q;
    let ans;

    let calc4 = calc(4, &mut p, [q, h, s, d]);
    let calc8 = calc(8, &mut p, [q, h, s, d]);
    if 2 * calc4 < calc8 {
        ans = n * calc4;
    } else {
        ans = n / 2 * calc8 + n % 2 * calc4;
    }
    println!("{}", ans);
}