Under construction

Still being built. Things may change

Two Machines

Interview

Time limit1sMemory limit512 MB

Summary
Given commissioning times and rates for two machines, find the most parts made in k minutes when only one engineer can commission at a time.
Level

Medium4 of 10

Topics
Greedy, Math
Solved
No attempts yet

Problem

A factory has two machines. The goal is to make as many parts as possible during today's shift, which lasts kk minutes.

Both machines are mothballed at the start. The first machine needs aa minutes to be commissioned, after which it makes xx parts per minute. The second machine needs bb minutes to be commissioned, after which it makes yy parts per minute.

An engineer must be present to commission a machine, so the two machines cannot be commissioned at the same time. Commissioning one machine while the other makes parts is allowed, and both machines may make parts at the same time.

Find the maximum number of parts that can be made in kk minutes.

Input

The first line contains a single integer kk (0≤k≤1090 \le k \le 10^9), the length of the shift in minutes.

The second line contains non-negative integers aa and xx (0≤a,x≤1090 \le a, x \le 10^9), the commissioning time of the first machine and the number of parts it makes per minute.

The third line contains non-negative integers bb and yy (0≤b,y≤1090 \le b, y \le 10^9), the commissioning time of the second machine and the number of parts it makes per minute.

Output

Print one integer, the maximum number of parts that can be made during the shift.

The answer may exceed the 32-bit integer range, so use a 64-bit type such as long long in C++ or int64 in Pascal.

Hint

In the sample, it is better to commission the second machine first and make 45 parts in the remaining 15 minutes. Then commission the first machine and make 20 more parts on it in the remaining 5 minutes.

If the first machine is commissioned first and makes 40 parts in the remaining 10 minutes, then the second machine can make only 15 parts after it is commissioned. The total is 55, which is less than 65.

Examples1

  1. Example 1

    Input
    20
    10 4
    5 3
    
    Expected output
    65