type Tree = { LeafNum: int; FallPerMonth: int } type Time = { Month: int; Elapsed: int } type Doing = { Tree: Tree; Time: Time } type Done = { Elapsed: int } type State = DOING of Doing | DONE of Done type Environment = { WindyStart: int; WindySpan: int } type Wind = Normal | Windy let isWindy env month = if env.WindyStart <= month && month < env.WindyStart + env.WindySpan then Windy else Normal let update env doing = let updateTree env month tree = match isWindy env month with | Windy -> { tree with LeafNum = tree.LeafNum - tree.FallPerMonth * 2 } | Normal -> { tree with LeafNum = tree.LeafNum - tree.FallPerMonth } let updateTime time = match time with | { Month = 12 }-> { Month = 1; Elapsed = time.Elapsed + 1 } | _ -> { Month = time.Month + 1; Elapsed = time.Elapsed + 1 } let newTree = updateTree env doing.Time.Month doing.Tree let newTime = updateTime doing.Time match newTree with | { LeafNum = x } when x <= 0 -> DONE { Elapsed = newTime.Elapsed } | _ -> DOING { Tree = newTree; Time = newTime } let simulate env tree = let rec f state = match state with | DONE x -> x.Elapsed | DOING x -> f <| update env x f <| DOING { Tree = tree; Time = { Month = 1; Elapsed = 0 } } let N, M, P, Q = let t = stdin.ReadLine().Split() |> Array.map int t.[0], t.[1], t.[2], t.[3] let tree = { LeafNum = N; FallPerMonth = M } let environment = { WindyStart = P; WindySpan = Q } simulate environment tree |> printfn "%i"