PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000274. Giảm nhỏ nhất giá trị lớn nhất của hai dãy (Minimize the Maximum of Two Arrays)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giảm nhỏ nhất giá trị lớn nhất của hai dãy (Minimize the Maximum of Two Arrays)

Minimize the Maximum of Two Arrays

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given divisors d1,d2d_1,d_2 and sizes c1,c2c_1,c_2, choose disjoint sets of positive integers A,BA,B of those sizes. No element of A is divisible by d1d_1, and no element of B is divisible by d2d_2. Minimize the greatest element of their union.

Input

One line contains d1,d2,c1,c2d_1,d_2,c_1,c_2.

Output

Print the optimal maximum value.

Subtasks

  • Subtask 1 (20 points): 2≤d1,d2≤82\le d_1,d_2\le8; 1≤c1,c2≤41\le c_1,c_2\le4; 2≤c1+c2≤82\le c_1+c_2\le8.
  • Subtask 2 (30 points): 2≤d1=d2≤1052\le d_1=d_2\le10^5; 1≤c1,c2<1091\le c_1,c_2<10^9; 2≤c1+c2≤1092\le c_1+c_2\le10^9.
  • Subtask 3 (50 points): 2≤d1,d2≤1052\le d_1,d_2\le10^5; 1≤c1,c2<1091\le c_1,c_2<10^9; 2≤c1+c2≤1092\le c_1+c_2\le10^9.

Examples

Input 1

2 4 8 2

Output 1

15

Explanation

Choose the eight odd numbers through 15 for A and 2,6 for B. Any smaller bound has at most seven odd numbers.