Under construction

Still being built. Things may change

Choosing Clothes

Time limit1sMemory limit1024 MB

Summary
Given the counts of tops, jeans, sweatpants, light innerwear, and dark innerwear, find the maximum number of days Yura can wear clothes without washing any item.
Level

Medium6 of 10

Topics
Math, Greedy, Implementation
Solved
No attempts yet

Problem

Yura's closet has AA tops, BB bottoms, and CC innerwear items. Every day, Yura picks one top, one bottom, and one innerwear item from the closet and wears them. The same top cannot be worn two days in a row. The same bottom can be worn two days in a row, but not three days in a row.

The bottoms are B1B_1 jeans and B2B_2 sweatpants (B1+B2=BB_1+B_2=B). The innerwear is C1C_1 light-colored items and C2C_2 dark-colored items (C1+C2=CC_1+C_2=C). Because of dye transfer, jeans and light-colored innerwear cannot be worn on the same day.

A top or bottom worn for 5 days becomes dirty and must be washed before it is worn again. Innerwear becomes dirty after just one day of wear and must be washed before it is worn again. Every top, bottom, and innerwear item in the closet is unworn at the start. Find how many days Yura can wear clothes from the closet without washing any of them.

Input

The first line contains an integer AA.

The second line contains two integers B1B_1 and B2B_2, separated by a space.

The third line contains two integers C1C_1 and C2C_2, separated by a space.

Output

Print the answer to the problem.

Constraints

0≤A,B1,B2,C1,C2≤1090 \leq A, B_1, B_2, C_1, C_2 \leq 10^9

Examples2

  1. Example 1

    Input
    3
    1 2
    5 6
    
    Expected output
    11
    
  2. Example 2

    Input
    4
    2 1
    6 5
    
    Expected output
    10