Official

A - Cabbages Editorial by en_translator


The answer depends on whether \(N\) is greater than \(A\).

1) If \(N \leq A\)

Each of \(N\) cabbages costs \(X\) yen, so we need \(NX\) yen in total to buy \(N\) cabbages.

2) If \(N > A\)

  • Each of the first \(A\) cabbages costs \(X\) yen, so they amount \(AX\) in total.
  • Each of the other cabbages costs \(Y\) yen, so it amount \((N-A)Y\) yen in total.

Thus, we need \(AX + (N-A)Y\) yen in total to buy \(N\) cabbages.

In order to implement the case division above, we can use the “conditional branch” features in programming languages. In C++ language, for instance the if statement works as a conditional branch.

The following is a sample code for this problem using C++ language.

#include <iostream>

using namespace std;

int main(void)
{
  int n, a, x, y;
  cin >> n >> a >> x >> y;

  if(n <= a) cout << n*x << endl;
  else cout << a*x + (n-a)*y << endl;

  return 0;
}

posted:
last update: