#include <bits/stdc++.h>
using namespace std;
int lt = 1000;
long long dp[1000001];
int main() {
  int n, hr, hb, wr, wb;
  while (scanf("%d", &n) != EOF) {
    scanf("%d%d%d%d", &hr, &hb, &wr, &wb);
    long long ans = 0;
    if (wr > lt) {
      for (int ct = 0; ct * wr <= n; ct++) {
        ans = max(ans, 1ll * ct * hr + 1ll * (n - ct * wr) / wb * hb);
      }
    } else if (wb > lt) {
      for (int ct = 0; ct * wb <= n; ct++) {
        ans = max(ans, 1ll * ct * hb + 1ll * (n - ct * wb) / wr * hr);
      }
    } else {
      memset(dp, 0, sizeof(dp));
      for (int i = wr; i <= wr * wb && i <= n; i++) {
        dp[i] = max(dp[i], dp[i - wr] + hr);
      }
      for (int i = wb; i <= wr * wb && i <= n; i++) {
        dp[i] = max(dp[i], dp[i - wb] + hb);
      }
      for (int i = 0; i <= wr * wb && i <= n; i++) {
        ans = max(ans, dp[i] + 1ll * (n - i) / wr * hr);
        ans = max(ans, dp[i] + 1ll * (n - i) / wb * hb);
      }
    }
    cout << ans << endl;
  }
}
