#include <bits/stdc++.h>
using namespace std;
const int N = 12;
long long sum, a, b, c, d;
long long cal(long long x) {
  long long s = 0, y = 0;
  s += x * b;
  y = (sum - x * d) / c;
  s += y * a;
  if (x < 0 || y < 0 || x * d + y * c > sum) return 0;
  return s;
}
long long fun() {
  long long x = 0, y = 0, s = 0;
  long long td = d % c;
  x = sum / d;
  y = (sum - x * d) / c;
  long long tsum = sum - x * d - y * c;
  if (td == 0) td = c;
  long long tx = (c - tsum) / td;
  long long md = (c - tsum) % td;
  long long l = sum / d - min(sum / d, tx), r = sum / d, mid, midr, i = 0;
  s = max(s, max(cal(r), cal(l)));
  s = max(s, max(cal(l), cal(l - 1)));
  while (l >= 0) {
    i++;
    if (i >= 1000000) break;
    md += c;
    tx = md / td;
    l -= tx;
    long long tmd = md % td;
    s = max(s, max(cal(l), cal(l - 1)));
    if (tmd == 0) break;
    md = tmd;
  }
  return s;
}
int main() {
  cin >> sum >> a >> b >> c >> d;
  long long s = 0, s1 = 0, s2 = 0, x = 0, y = 0;
  s = max(s, fun());
  swap(a, b);
  swap(c, d);
  s = max(s, fun());
  cout << s << endl;
  return 0;
}
