Under construction

Still being built. Things may change

Tables and Legs

Time limit2sMemory limit1024 MB

Summary
Given how many square tables have 0 to 4 legs, find the minimum minutes to unscrew and rescrew legs so that no table is left with 1, 2, or 3 legs.
Level

Medium6 of 10

Topics
Greedy, Math
Solved
No attempts yet

Statement

A factory makes square tables with four legs each. Its conveyor that screws legs onto tables broke unexpectedly. The old conveyor cannot be repaired, so a new machine was bought. The new machine only works on tables with no legs and outputs finished tables with four legs. As a result, tables with one, two, or three legs are no longer useful.

To avoid throwing away nearly finished products, an experimental robot is used. Its goal is to unscrew legs from some tables and screw them onto others, so that the number of tables with one, two, or three legs becomes zero.

In one minute, the robot can either unscrew one leg from a table, or screw one previously removed leg onto another table. Find the minimum time the robot needs to complete the task.

Input

The first line contains five integers a0,a1,a2,a3,a4a_0, a_1, a_2, a_3, a_4 separated by spaces. aia_i is the number of tables with ii legs (0≤ai≤10180 \le a_i \le 10^{18}).

Output

Print one number: the answer to the problem.

Examples1

  1. Example 1

    Input
    1 1 0 2 1
    
    Expected output
    5