package main import . "fmt" import . "os" import bf "bufio" import . "slices" func main() { sc:=bf.NewScanner(Stdin) wr:=bf.NewWriter(Stdout) var n int sc.Scan() Sscan(sc.Text(),&n) m:=make(map[string]int64) p:=make([]string,n) for i:=range p { sc.Scan() s:=sc.Text() c:=int64(i+1)*int64(n-i) p[i]=s m[s]+=c } Sort(p) p=Compact(p) for _,s:=range p { Fprintln(wr,m[s],s) } wr.Flush() }