Bessie's Function
시간 제한2초메모리 제한2048 MB
원소마다 변경 비용이 주어진 함수에서 f(f(x)) = f(x)가 모든 x에 대해 성립하도록 최소 비용으로 값을 바꾸는 문제입니다.
문제
Bessie has a special function that takes as input an integer in and returns an integer in (). Her function is defined by integers where ().
Bessie wants this function to be idempotent. In other words, it should satisfy for all integers .
For a cost of , Bessie can change the value of to any integer in (). Determine the minimum total cost Bessie needs to make idempotent.
입력
The first line contains .
The second line contains space-separated integers .
The third line contains space-separated integers .
출력
Output the minimum total cost Bessie needs to make idempotent.