#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
pair<long long, long long> px, py;
long long dp[1000001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n >> px.second >> py.second >> px.first >> py.first;
  if (px.first < py.first) swap(px, py);
  if (px.first >= 20) {
    long long res = 0;
    for (long long i = 0; i * px.first <= n; i++) {
      long long v = n - i * px.first;
      v /= py.first;
      res = max(res, i * px.second + v * py.second);
    }
    cout << res << endl;
    return 0;
  }
  long long res = 0;
  for (int i = 1; i <= 1000000; i++) {
    if (i > n) break;
    if (i >= px.first) dp[i] = max(dp[i], dp[i - px.first] + px.second);
    if (i >= py.first) dp[i] = max(dp[i], dp[i - py.first] + py.second);
    long long c = n / i;
    long long v = n - c * i;
    res = max(dp[i] * c + dp[v], res);
  }
  cout << res << endl;
}
