#include <bits/stdc++.h>
using namespace std;
long long gcr, gcb;
long long onesolve(long long c, long long a, long long b, long long L,
                   long long U) {
  if (c <= 0)
    return max(0LL, L);
  else
    return min(U, b / a);
}
long long eval(pair<long long, long long> x, bool flip) {
  if (flip) swap(x.first, x.second);
  return x.first * gcr + x.second * gcb;
}
pair<long long, long long> combine(pair<long long, long long> lhs,
                                   pair<long long, long long> rhs) {
  return pair<long long, long long>(lhs.first + rhs.first,
                                    lhs.second + rhs.second);
}
pair<long long, long long> rev(pair<long long, long long> v) {
  return pair<long long, long long>(v.second, v.first);
}
long long rupdiv(long long a, long long b) {
  return (long long)(ceil(a / (long double)b));
}
pair<long long, long long> KP(long long c1, long long c2, long long a1,
                              long long a2, long long b, long long L,
                              long long U) {
  if (b < 0)
    return pair<long long, long long>(-9000000000000000000LL,
                                      -9000000000000000000LL);
  if (c1 <= 0)
    return pair<long long, long long>(
        L, onesolve(c2, a2, b - a1 * L, 0, 9000000000000000000LL));
  if (c2 <= 0) return pair<long long, long long>(onesolve(c1, a1, b, L, U), 0);
  if (a1 == 0)
    return pair<long long, long long>(
        U, onesolve(c2, a2, b, 0, 9000000000000000000LL));
  if (a2 == 0) return pair<long long, long long>(0, 9000000000000000000LL);
  if (L == U)
    return pair<long long, long long>(
        L, onesolve(c2, a2, b - a1 * L, 0, 9000000000000000000LL));
  if (b == 0) return pair<long long, long long>(0, 0);
  if (U != 9000000000000000000LL) U = min(U, b / a1);
  if (L != 0 || U != 9000000000000000000LL) {
    pair<long long, long long> v1 = pair<long long, long long>(
                                   U, (b - a1 * U) / a2),
                               v2 = combine(
                                   KP(c1, c2, a1, a2,
                                      b - rupdiv(b - a1 * U, a2) * a2 - a1 * L,
                                      0, 9000000000000000000LL),
                                   pair<long long, long long>(
                                       L, rupdiv(b - a1 * U, a2)));
    return (c1 * v1.first + c2 * v1.second > c1 * v2.first + c2 * v2.second
                ? v1
                : v2);
  }
  if (a1 < a2) return rev(KP(c2, c1, a2, a1, b, 0, 9000000000000000000LL));
  long long k = a1 / a2;
  long long p = a1 - k * a2;
  pair<long long, long long> kp =
      KP(c1 - c2 * k, c2, p, a2, b - k * (b / a1) * a2, 0, b / a1);
  pair<long long, long long> ret =
      combine(pair<long long, long long>(kp.first, kp.second - k * kp.first),
              pair<long long, long long>(0, k * (b / a1)));
  return ret;
}
int main() {
  long long C, Hr, Hb, Wr, Wb;
  cin >> C >> Hr >> Hb >> Wr >> Wb;
  gcr = Hr;
  gcb = Hb;
  pair<long long, long long> s =
      KP(Hr, Hb, Wr, Wb, C, 0, 9000000000000000000LL);
  cout << (Hr * s.first + Hb * s.second) << endl;
  return 0;
}
