Temple Build

Time limit3sMemory limit128 MB

Summary
Given an ideal truncated square pyramid and three brick cube sizes, stack square layers fully inside the shape to maximize total volume.
Level

Medium7 of 10

Topics
Dynamic programming, Math, Geometry, Implementation
Solved
No attempts yet

Problem

The dwarves are master builders. They discovered that, above ground, the structures most resistant to attack were truncated square pyramids: a square pyramid that does not rise all the way to a point but is instead cut off with a flat square top.

Knowing the height they wanted and the side lengths of the square base at the bottom and at the top, the dwarves fixed the ideal shape of a building. They had three sizes of cubic bricks to work with, and their goal was to maximize the building's volume under the following rules.

The building is assembled from layers. Each layer is a single square made of bricks of one size: a layer built from bricks of side cc is an n×nn \times n arrangement of those cubes, so it has height cc and a square footprint of side n⋅cn \cdot c. No part of any brick may stick out of the ideal shape, either through the sides or above the top. The finished structure has jagged sides and may be shorter than the ideal shape, but every brick must lie completely inside the ideal design.

The ideal shape's cross-section is a square whose side length changes linearly with height, equal to the bottom base at height 00 and to the top base at the ideal height, and it is centered on the vertical axis. Layers are stacked contiguously starting from the ground. There is no limit on how many bricks of each size may be used.

Print the maximum total volume that can be achieved.

Input

Each line contains six integers separated by single spaces: the ideal height of the building, the side length of the square base at the bottom, and the side length of the square base at the top (these three are non-negative integers, each at most 1,000,000), followed by the side lengths of the three available cubic bricks (each a non-negative integer at most 10,000). Input continues until end of file.

Output

For each line of input, print on its own line the maximum volume that can be built under the rules.

Examples3

  1. Example 1

    Input
    500000 800000 300000 6931 11315 5000
    
    Expected output
    160293750000000000
    
  2. Example 2

    Input
    10 10 10 3 3 3
    
    Expected output
    729
    
  3. Example 3

    Input
    20 5 40 3 3 3
    
    Expected output
    6345