main=interact $ unlines.f[].map read.words f [] (x:rs)=show x:f[x]rs f _ [] = [] f ls (x:rs)=let (u,s) = break(<x-9)ls (d,t) = break(<x-9)rs in if null s || null t then show x:f(x:ls)rs else shows x(if null [a|a<-u++d,a>x] then "*" else ""):f(x:ls)rs
Note that non-ascii characters in the above source code will be escaped (such as \x9f).