Problem list
Log in
#coci00701 · COCI 2025/2026 Contest 1

Krugomet

Unrated 1.0 s 512 MB

$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

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 1000$), 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

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.

Example 1

Input
2 1
5 6
2 1
Output
6
1

Example 2

Input
4 2
5 5 5 5
1 2 1 1
Output
15
1

Example 3

Input
4 10000000
1 2 3 4
2 1 4 3
Output
4
4

Clarification of the first example: 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.

Source
COCI 2025/2026 Contest 1
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code