import sys def main(): T = int(sys.stdin.readline()) N = int(sys.stdin.readline()) c = list(map(int, sys.stdin.readline().split())) v = list(map(int, sys.stdin.readline().split())) attractions = [] for i in range(N): current_c = c[i] current_v = v[i] options = [(0, 0)] current_time = current_c total_value = current_v if current_time <= T: options.append((current_time, total_value)) next_v = current_v // 2 while next_v > 0 and current_time + current_c <= T: current_time += current_c total_value += next_v options.append((current_time, total_value)) next_v = next_v // 2 sorted_options = sorted(options, key=lambda x: x[0]) pruned = [] max_val = -1 for t, val in sorted_options: if val > max_val: pruned.append((t, val)) max_val = val attractions.append(pruned) dp_prev = [-1] * (T + 1) dp_prev[0] = 0 for attraction in attractions: new_dp = [x for x in dp_prev] for t_opt, val_opt in attraction: for t in range(T, t_opt - 1, -1): if dp_prev[t - t_opt] != -1: if new_dp[t] < dp_prev[t - t_opt] + val_opt: new_dp[t] = dp_prev[t - t_opt] + val_opt dp_prev = new_dp max_val = max([v for v in dp_prev if v != -1]) print(max_val) if __name__ == "__main__": main()