https://www.acmicpc.net/problem/1459
I only thought of 2 cases
but there is additional case. When going straight is more expensive, we can go diagonally and there are 2 ways.
if x+y %2==0, then we can just take diagonal paths and do cost2 = max(X, Y) D. But if it is not, we have to take 1 straight path after going as much diagonally as possible so like cost2 = (max(X, Y) - 1) D + C
https://island-developer.tistory.com/115
so the correct solution is
X, Y, C, D = map(int, input().split())
cost1 = (X + Y) * C
if (X + Y) % 2 == 0:
cost2 = max(X, Y) * D
else:
cost2 = (max(X, Y) - 1) * D + C
cost3 = min(X, Y) * D + abs(X - Y) * C
print(min(cost1, cost2, cost3))
The time complexity of this code is O(1) because it performs a fixed number of arithmetic operations and condition checks regardless of the input values. It doesn't involve any loops or recursive calls that depend on the input size.
The space complexity is also O(1) because it uses a constant amount of memory to store variables and intermediate results, regardless of the input values.