COCI '25 Contest 1 #2 Krugomet

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

n students gathered in the schoolyard one sunny day and stood in a large circle. Each person had his own box of balls (person i has a_i balls), as well as his crush (maybe even himself), denoted by s_i.

They decided to play their favorite game, krugomet. Playing krugomet consists of k rounds. In each round, all participants in the game simultaneously throw all their balls towards their crush. The students are skilled and agile, so they will always catch all the balls thrown to them in a round. After the balls are caught, everyone gets ready for the next round, and the whole process is repeated.

The teacher fell asleep on the bench and left the students to play alone. When he woke up, the students asked him to tell them who won, i.e. to answer 2 questions:

  • How many balls does the student with the most balls have after k rounds of play?
  • Who collected the most balls after k rounds of play?

The teacher knows how many students played krugomet n, the sequence of crushes s_i , the initial number of balls for each student a_i , and how many rounds of play k were played. Unfortunately, the teacher doesn't have time to count how many balls each person has, so he leaves that up to you.

Help him and determine the list of students who collected the most balls. In case there is more than one person with the most balls, print their indices in order from smaller to larger.

Input Specification

In the first line are the two natural numbers n and k (1 \le n \le 10^5, 1 \le k \le 10^9) from the text of the task.

In the second line, there are n natural numbers a_i (1 \le a_i \le 1\,000), numbers from the text of the task.

In the third line are n natural numbers s_i (1 \le s_i \le n), numbers from the text of the task.

Output Specification

In the first line, print a single number - the number of balls collected by the student with the most balls.

In the second line, print the indices of the students who collected the most balls. The indices should be printed in ascending order.

Constraints

Subtask Points Constraints
1 14 n, k \le 1\,000
2 26 The sequence s is a permutation, s_i \ne s_j (1 \le i < j \le n).
3 30 No additional constraints.

Correctly printing the 1st line earns 50\% points for the test example. The remaining 50\% points for the test example are earned by correctly printing the 2nd line.

Sample Input 1

2 1
5 6
2 1

Sample Output 1

6
1

Explanation for Sample Output 1

In the only round of krugomet, people labeled 1 and 2 threw all their balls towards each other and "swapped" how many balls each person had. After the first round, person labeled 1 had the most balls and was holding 6 balls.

Sample Input 2

4 2
5 5 5 5
1 2 1 1

Sample Output 2

15
1

Sample Input 3

4 10000000
1 2 3 4
2 1 4 3

Sample Output 3

4
4

Comments

There are no comments at the moment.