This page is still under construction.

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

Small Schedule

Interview

Time limit1sMemory limit512 MB

Summary
Given M identical machines, S jobs of length 1 and L jobs of length Q, find the minimum makespan to schedule all jobs.
Level

Medium6 of 10

Topics
Binary search, Greedy, Math, Implementation
Solved
No attempts yet

Problem

Everybody is into cloud computing these days, so quite a few different business models are being experimented with. You are trying a very simple one: you sell time on your machines in one of two batches called slots. A customer can buy one second of CPU time or QQ seconds for some integer QQ.

Each time slot a customer purchases must be completed on a single machine, but you get to decide how to allocate the purchased time slots between machines.

After coming back from a long vacation, you see that all of your machines are idle and a variety of orders have come in. To keep customers happy, you must decide how to distribute these requests between machines in a way that minimizes the time when the purchased time slots are finally all completed.

What is the smallest amount of time in which you can complete all of the purchased time slots?

Input

The input consists of a single line containing four integers QQ (2≤Q≤1 0002 \leq Q \leq 1\,000), which is the time needed to complete the longer batches, MM (1≤M≤1 000 0001 \leq M \leq 1\,000\,000), which is the number of machines owned by your company, SS (0≤S≤1 000 0000 \leq S \leq 1\,000\,000), which is the number of 1-second time slots purchased, and LL (0≤L≤1 000 0000 \leq L \leq 1\,000\,000), which is the number of QQ-second time slots purchased.

Output

Display the smallest amount of time in which you can complete all of the purchased time slots.

Examples3

  1. Example 1

    Input
    2 4 3 6
    
    Expected output
    4
    
  2. Example 2

    Input
    3 4 3 5
    
    Expected output
    6
    
  3. Example 3

    Input
    10 2 0 1
    
    Expected output
    10