public override IEnumerable <object> Solve(TextReader inputStream) { var n = inputStream.ReadLong(); var(gA, sA, bA) = inputStream.ReadValue <int, int, int>(); var(gB, sB, bB) = inputStream.ReadValue <int, int, int>(); var metals = new Metal[] { new Metal(gA, gB), new Metal(sA, sB), new Metal(bA, bB) }; Array.Sort(metals); long max = n; if (metals.All(m => m.BuyAtA) || metals.All(m => m.BuyAtB)) { // どちらかで3個買って3個全部売る for (long metal0 = 0; metal0 <= n / metals[0].Buy; metal0++) { var payed0 = metals[0].Buy * metal0; for (long metal1 = 0; metal1 <= (n - payed0) / metals[1].Buy; metal1++) { var payed1 = metals[1].Buy * metal1; var metal2 = (n - payed0 - payed1) / metals[2].Buy; max = Math.Max(max, n + metal0 * metals[0].Profit + metal1 * metals[1].Profit + metal2 * metals[2].Profit); } } } else if (metals[1].BuyAtA) { // Aで2個、Bで1個買う for (long metal0 = 0; metal0 <= n / metals[0].Buy; metal0++) { long wallet = n; var payed0 = metals[0].Buy * metal0; var metal1 = (wallet - payed0) / metals[1].Buy; wallet += metal0 * metals[0].Profit + metal1 * metals[1].Profit; var metal2 = wallet / metals[2].Buy; wallet += metal2 * metals[2].Profit; max = Math.Max(max, wallet); } } else { // Aで1個、Bで2個買う var metal0 = n / metals[0].Buy; var initialWallet = n + metal0 * metals[0].Profit; for (long metal1 = 0; metal1 <= initialWallet / metals[1].Buy; metal1++) { var payed1 = metal1 * metals[1].Buy; var metal2 = (initialWallet - payed1) / metals[2].Buy; max = Math.Max(max, initialWallet + metal1 * metals[1].Profit + metal2 * metals[2].Profit); } } yield return(max); }