PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000019. Tổng và nhỏ nhất trên hành trình (Analysis of Pathes in Functional Graph)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng và nhỏ nhất trên hành trình (Analysis of Pathes in Functional Graph)

Analysis of Pathes in Functional Graph

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn vertices numbered 00 to n−1n-1. Each vertex uu has exactly one directed outgoing edge to fuf_u, carrying weight wuw_u.

For every starting vertex uu, follow exactly KK outgoing edges. Compute the sum of weights and the minimum weight among those KK traversed edges.

A vertex or edge can be visited multiple times; each traversal contributes to the sum.

Input

  • The first line contains n,Kn,K.
  • The second line contains nn destination indices f0,…,fn−1f_0,\ldots,f_{n-1}.
  • The third line contains nn outgoing-edge weights w0,…,wn−1w_0,\ldots,w_{n-1}.

Output

Print nn lines for starting vertices 0,1,…,n−10,1,\ldots,n-1. Each line contains the sum of weights and the minimum weight, separated by a space.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; 1≤n≤1051\le n\le10^5; 1≤K≤10101\le K\le10^{10}; 0≤fi<n0\le f_i<n; 0≤wi≤1080\le w_i\le10^8.
  • Subtask 2 (30 points): n≤1000n\le1000; 1≤n≤1051\le n\le10^5; 1≤K≤10101\le K\le10^{10}; 0≤fi<n0\le f_i<n; 0≤wi≤1080\le w_i\le10^8.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤K≤10101\le K\le10^{10}; 0≤fi<n0\le f_i<n; 0≤wi≤1080\le w_i\le10^8.

Examples

Input

7 3
1 2 3 4 3 2 6
6 3 1 4 2 2 3

Output

10 1
8 1
7 1
10 2
8 2
7 1
9 3

Explanation

Starting at vertex 00, the next three traversed edges have weights 6,3,16,3,1, so their sum is 1010 and the minimum is 11. The remaining lines correspond to other starting vertices.