ll@N,@M,@A[N],@B[M],@C[N],D[N]{},s=0,a=1e18;mapc;REP(i,N-1)A[i+1]-=D[i+1]=D[i]+B[i%M];rep(i,N)s+=C[i],c[A[i]]+=C[i];auto j=c.begin(),e=c.end();for(;j!=e;j++)a>?=j->second;wt(s-a);