arc109_a - Hands

Divide problem on cases:

  1. a == b. You will need to use a at least one corridor, so might as well use just one.
  2. a > b and a < b. Two options:
    • Only use corridors (zig-zag).
    • Use one corridor and then stairs.

Time complexity: $O(1)$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int a, b, x, y;
    cin >> a >> b >> x >> y;
    if (a == b)
    {
        cout << x << endl;
    }
    else if (a > b)
        cout << min(x + (a - 1 - b) * y, x * ((a - b) * 2 - 1)) << endl;
    else
        cout << min(x + (b - a) * y, x * ((b - a) * 2 + 1)) << endl;
    return 0;
}