CodeforcesMay 26, 2025

Greedy Arkady

Hazrat Ali

Codeforces

The people are numbered from 11 to kk, and Arkady is the first of them. To split the candies, Arkady will choose an integer xx and then give the first xx candies to himself, the next x candies to the second person, the next xx candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by xx) will be thrown away.

Arkady can't choose x greater than MM as it is considered greedy. Also, he can't choose such a small xx that some person will receive candies more than DD times, as it is considered a slow splitting.

Please find what is the maximum number of candies Arkady can receive by choosing some valid xx.

Input

The only line contains four integers nnkkMM and DD (2n10182≤n≤10182kn2≤k≤n1Mn1≤M≤n1Dmin(n,1000)1≤D≤min(n,1000)MDknM⋅D⋅k≥n) — the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies.

Output

Print a single integer — the maximum possible number of candies Arkady can give to himself.

Note that it is always possible to choose some valid xx.

Examples
Input
20 4 5 2
Output
8
Input
30 9 4 1
Output
4


Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, M, D;
  cin >> n >> k >> M >> D;
  if (n <= M) {
    cout << n << endl;
    return 0;
  }
  long long low, mid, high;
  low = n / (k * D);
  high = M;
  mid = (low + high) / 2;
  while (low != high) {
    mid = (low + high) / 2;
    if (mid > low) {
      low = mid;
    } else {
      high = mid;
    }
  }
  cout << mid * D  << endl;
  return 0;
}




Comments