import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { auto t = readln.chomp.to!int; auto n = readln.chomp.to!size_t; auto ci = readln.split.map!(to!int).array; auto vi = readln.split.map!(to!int).array; foreach (i; 0..n) { for (auto v = vi[i] / 2; v > 0; v /= 2) { ci ~= ci[i]; vi ~= v; } } n = ci.length; auto memo = new int[](t + 1); foreach (i; 0..n) { auto c = ci[i], v = vi[i]; foreach_reverse (j; c..t + 1) memo[j] = max(memo[j], memo[j - c] + v); } writeln(memo.reduce!(max)); }