#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a rank,p; vector sz; DisjointSet(){} DisjointSet(int size){ rank.resize(size,0); p.resize(size,0); sz.resize(size,0); for(int i=0;irank[y]){ p[y]=x; sz[x]+=sz[y]; }else{ p[x]=y; sz[y]+=sz[x]; if(rank[x]==rank[y]){ rank[y]++; } } } int findSet(int x){ if(x!=p[x]){ p[x]=findSet(p[x]); } return p[x]; } int findSize(int x){ return sz[findSet(x)]; } }; int N,K; vector P; vector> g; vector> rg; vector last,used,in,dist; vector ans; void dfs(int now,vector &v,int M,vector &sum,int i){ v.push_back(last[now]); for(auto nex:rg[now]){ if(used[nex]){ dist[nex]=dist[now]+1; last[nex]=last[now]+P[nex]; { if(dist[nex]>=K){ ans[nex]=last[nex]-v[(int)v.size()-1-K]; }else{ ans[nex]+=last[nex]; ll rest=K-dist[nex]; ans[nex]+=sum[M]*(rest/M); ans[nex]+=sum[i+rest%M]-sum[i]; } } dfs(nex,v,M,sum,i); } } v.pop_back(); } void solve(int col,vector &nodes){ int n=nodes.size(); queue Q; for(auto p:nodes) if(in[p]==0) Q.push(p); while(!Q.empty()){ int now=Q.front();Q.pop(); used[now]=1; for(auto nex:g[now]){ in[nex]--; if(in[nex]==0) Q.push(nex); } } vector circuit; int st=0; for(auto p:nodes) if(used[p]==0) st=p; Q.push(st); while(!Q.empty()){ int now=Q.front(); Q.pop(); circuit.push_back(now); for(auto nex:g[now]){ if(nex!=st) Q.push(nex); } } int M=circuit.size(); vector sum(2*M+1,0); for(int i=0;i<2*M;i++) sum[i+1]=sum[i]+P[circuit[i%M]]; for(int i=0;i v; v.push_back(0); dfs(p,v,M,sum,i); } for(int i=0;i>N>>K; P.resize(N); rep(i,N) cin>>P[i]; g.resize(N); rg.resize(N); last.resize(N,0); used.resize(N,0); in.resize(N,0); dist.resize(N,MOD); ans.resize(N,0); DisjointSet us(N); for(int i=0;i> nodes(N); for(int i=0;i