루미 알고리즘

#1621실버Kattis Speedrun

PO 2025 온라인 예선코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Joshua's New Year's resolution for 2024 was to solve half of all problems on the Kattis platform before the year ends. As the year draws to a close, he realizes he'll need to use every strategy he knows to reach his goal. So far, he has only solved others' problems on Kattis. However, he can also create his own problem, upload it, and solve it. While this might involve less work than solving someone else's problem, it also increases the total number of problems.

Currently, there are P problems on Kattis, and Joshua has solved S of them. He can take the following actions:

  • Solve a problem. This takes A seconds and increases S by 1.
  • Write a new problem. This takes B seconds and increases both P and S by 1.

If Joshua uses his time optimally, how many seconds will it take for him to increase S and P such that S/P > 0.5?

Input

The first line of input contains the integer S (0 ≤ S ≤ 10^5), the number of problems Joshua has solved so far.

The second line contains the integer P (1 ≤ P ≤ 10^5), the total number of problems currently on Kattis. It is guaranteed that S < P/2.

The third line contains the integer A (1 ≤ A ≤ 10^5), the time it takes to solve a problem.

The fourth line contains the integer B (1 ≤ B ≤ 10^5), the time it takes to create a new problem.

Note that in some test cases, A may be much larger than B, while in others B may be much larger than A.

Output

Print an integer: the minimum number of seconds it will take for Joshua to solve strictly more than half of all problems on Kattis.

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

GroupPointsConstraints
112A=1, B=10^5
218B=1, A=10^5
320S, P ≤ 1000
450No additional constraints.

Explanation of Samples

In sample 1, solving problems is very fast, while creating new problems takes a very long time. The optimal strategy is to only solve problems. To solve more than half, he needs to solve 51 problems. Since A=1, the answer is 51.

In sample 2, Joshua needs to solve 2 problems to solve more than half.

In sample 3, the optimal strategy is to solve 11 problems and create 1 problem. This takes a total of 11 · 7 + 1 · 6 = 83 seconds. No other strategy is faster.

예제

예제 1

입력
0
100
1
100000
출력
51

예제 2

입력
0
3
1
100000
출력
2

예제 3

입력
4
30
7
6
출력
83

출처 표기

출처: Programmeringsolympiaden 2025년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.