This page is still under construction.

Parts of this page are still being built. What you see may change.

Lazy Running

Time limit1sMemory limit256 MB

Summary
Starting and ending at checkpoint p2 on a 4-cycle, find the shortest closed walk whose swipe-recorded total distance is at least K, given the four edge lengths.
Level

Hard8 of 10

Topics
Shortest path, Dynamic programming, Math, Greedy
Solved
No attempts yet

Problem

At HD University, you have to be able to run around the campus 24 times in a row. Otherwise, you will fail the physical education exam and get expelled from the university. According to the rules, you must keep your speed, and your total running distance should be at least KK meters.

There are four checkpoints on the campus, labeled p_1p\_1, p_2p\_2, p_3p\_3 and p_4p\_4. Every time you pass a checkpoint, you should swipe your card, and the distance between this checkpoint and the last checkpoint you passed is added to your total distance.

The system regards the four checkpoints as a circle: from checkpoint p_ip\_i, you can only run to one of its neighbors, p_i−1p\_{i - 1} or p_i+1p\_{i + 1}; p_1p\_1 and p_4p\_4 are also neighbors of each other. You can run along a straight or curved line between neighboring checkpoints, but it makes no difference for the system: only the distance between checkpoints is taken into account.

Checkpoint p_2p\_2 is the nearest to the dormitory, so Little Q always starts and ends running at this checkpoint. Write a program to help Little Q find the shortest path such that the total running distance taken into account by the system is at least KK meters.

Input

The first line of the input contains five integers KK, d_1,2d\_{1, 2}, d_2,3d\_{2, 3}, d_3,4d\_{3, 4} and d_4,1d\_{4, 1} denoting the required distance and the distances between every pair of neighboring checkpoints (1≤K≤10181 \leq K \leq 10^{18}, 1≤d≤3⋅1041 \leq d \leq 3 \cdot 10^4).

Output

Print a single line containing a single integer: the length of the shortest path.

Examples1

  1. Example 1

    Input
    2000 600 650 535 380
    
    Expected output
    2165