// No.2099 [Cherry Alpha B] Time Machine package main import ( "fmt" "math" ) func main() { var T, X, A, B, Y int for { fmt.Scan(&T) fmt.Scan(&X, &A) fmt.Scan(&Y, &B) var t, d int if T >= 0 { i := T / A t = T % A d = i * X } else { i := (-T + B - 1) / B t = T + i*B d = i * Y } mind := math.MaxInt var f func(t, d int) f = func(t, d int) { if d > mind { return } if t == 0 { if d < mind { mind = d } return } if t > 0 { f(0, d+t) f(t-A, d+X) } else { f(t+B, d+Y) } } f(t, d) fmt.Println(mind) } }